Advertisement
Alex_tz307

USACO 2019 February Contest, Silver Problem 3. The Great Revegetation

Apr 18th, 2021
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("revegetate.in");
  6. ofstream fout("revegetate.out");
  7.  
  8. const int NMAX = 1e5 + 5;
  9. vector<int> G1[NMAX], G2[NMAX];
  10. int tag[NMAX];
  11. bool flag;
  12.  
  13. void dfs(int u) {
  14.   if (flag)
  15.     return;
  16.   for (int v : G1[u]) {
  17.     if (tag[v] == 3 - tag[u])
  18.       flag = true;
  19.     if (tag[v] == 0) {
  20.       tag[v] = tag[u];
  21.       dfs(v);
  22.     }
  23.   }
  24.   for (int v : G2[u]) {
  25.     if (tag[v] == tag[u])
  26.       flag = true;
  27.     if (tag[v] == 0) {
  28.       tag[v] = 3 - tag[u];
  29.       dfs(v);
  30.     }
  31.   }
  32. }
  33.  
  34. void solve() {
  35.   int N, M;
  36.   fin >> N >> M;
  37.   for (int i = 0; i < M; ++i) {
  38.     char c;
  39.     int u, v;
  40.     fin >> c >> u >> v;
  41.     if (c == 'S') {
  42.       G1[u].emplace_back(v);
  43.       G1[v].emplace_back(u);
  44.     } else {
  45.       G2[u].emplace_back(v);
  46.       G2[v].emplace_back(u);
  47.     }
  48.   }
  49.   int ans = 0;
  50.   for (int u = 1; u <= N && !flag; ++u)
  51.     if (!tag[u]) {
  52.       ++ans;
  53.       tag[u] = 1;
  54.       dfs(u);
  55.     }
  56.   if (flag)
  57.     fout << "0\n";
  58.   else {
  59.     fout << '1';
  60.     for (int i = 0; i < ans; ++i)
  61.       fout << '0';
  62.     fout << '\n';
  63.   }
  64. }
  65.  
  66. void close_files() {
  67.   fin.close();
  68.   fout.close();
  69. }
  70.  
  71. int main() {
  72.   solve();
  73.   close_files();
  74.   return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement