Advertisement
tepyotin2

Sol-P65 The Great Revegetation #920

Nov 7th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     freopen("revegetate.in", "r", stdin);
  5.     freopen("revegetate.out", "w", stdout);
  6.    
  7.     int n, m;
  8.     cin >> n >> m;
  9.     vector<vector<pair<int, bool>>> adj(n);
  10.    
  11.     for (int i = 0; i < m; i++) {
  12.         char type;
  13.         int u, v;
  14.         cin >> type >> u >> v;
  15.        
  16.         adj[--u].push_back({--v, type == 'S'});
  17.         adj[v].push_back({u, type == 'S'});
  18.     }
  19.     int component_num = 0;
  20.     bool impossible = false;
  21.     vector<int> color(n, -1);
  22.     for (int i = 0; i < n; i++) {
  23.         // haven't visited this node yet.
  24.         if (color[i] == -1) {
  25.             component_num++;
  26.            
  27.             queue<pair<int, bool>> todo;
  28.             todo.push({i, true});
  29.             while (!todo.empty()) {
  30.                 // process next node
  31.                 pair<int, bool> nxt = todo.front();
  32.                 todo.pop();
  33.                 // set grass type for nxt.first
  34.                 color[nxt.first] = nxt.second;
  35.                 // iterate through adjacent nodes
  36.                 for (pair<int, bool> u : adj[nxt.first]) {
  37.                     bool type = u.second ? nxt.second : !nxt.second;
  38.                    
  39.                     // haven't visited
  40.                     if (color[u.first] == -1) {
  41.                         todo.push({u.first, type});
  42.                     // creates a contradiction
  43.                     } else if (color[u.first] == !type) {
  44.                         impossible = true;
  45.                         break;
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     if (impossible) {
  53.         cout << 0 << endl;
  54.     } else {
  55.         // 2^component_num in binary is 1, followed by component_num zeros.
  56.         cout << 1;
  57.         for (int i = 0; i < component_num; i++) {
  58.             cout << 0;
  59.         }
  60.         cout << endl;
  61.     }
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement