Advertisement
tepyotin2

P65 The Great Revegetation #920

Nov 7th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. long long component = 0;
  6. bool impossible = false;
  7. vector<pair<int, char>> adj[100001];
  8. bool visited[100001];
  9. bool color[100001];
  10. bool isDiag = false;
  11. void dfs(int u, bool g){
  12.    
  13.     visited[u] = true;
  14.     color[u] = g;
  15.    
  16.     for (auto v: adj[u]){
  17.    
  18.         if(visited[v.first]){
  19.             if(v.second == 'S' && color[v.first] != g) impossible = true;
  20.             if(v.second == 'D' && color[v.first] == g) impossible = true;
  21.         }
  22.  
  23.         if(!visited[v.first]){
  24.             if(v.second == 'S') dfs(v.first, g);
  25.             else dfs(v.first, !g);
  26.         }
  27.    
  28.     }
  29. }
  30.  
  31. int main()
  32. {  
  33.     //isDiag = true;
  34.     freopen("revegetate.in", "r", stdin);
  35.    
  36.    
  37.     int n, m;
  38.     cin >> n >> m;
  39.     if(isDiag) cout << "n: " << n << ", m: " << m << endl;
  40.     for(int i = 0; i < m; ++i){
  41.         char a;
  42.         int b, c;
  43.         cin >> a >> b >> c;
  44.         adj[b].push_back({c, a});
  45.         adj[c].push_back({b, a});
  46.     }
  47.  
  48.     for(int i = 1; i <= n; ++i){
  49.         if(!visited[i]){
  50.             component++;
  51.             dfs(i, 0);
  52.         }
  53.         if(impossible) break;
  54.     }
  55.  
  56.     if(!isDiag) freopen("revegetate.out", "w", stdout);
  57.     if(impossible) cout << 0 << endl;
  58.     else{
  59.         cout << 1;
  60.         while(component--){
  61.             cout << 0;
  62.         }
  63.     }
  64.  
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement