Advertisement
mickypinata

CUBE-T224: Lion Army

Jul 3rd, 2021
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5.  
  6. const int MOD = 1e9 + 7;
  7. const int N = 3e5;
  8. const int logN = log2(N);
  9.  
  10. int dp[logN + 1], ds[N + 1], sz[N + 1];
  11. bool isWith[N + 1];
  12.  
  13. int powerOf2(int x){
  14.     int ans = 1;
  15.     for(int i = logN; i >= 0; --i){
  16.         if(x >= (1 << i)){
  17.             ans = (lli)ans * dp[i] % MOD;
  18.             x -= (1 << i);
  19.         }
  20.     }
  21.     return ans;
  22. }
  23.  
  24. bool fusion(bool a, bool b){
  25.     return ((int)a + (int)b) % 2 == 0;
  26. }
  27.  
  28. int dsFind(int u){
  29.     if(ds[u] == u){
  30.         return u;
  31.     }
  32.     int p = dsFind(ds[u]);
  33.     isWith[u] = fusion(isWith[u], isWith[ds[u]]);
  34.     return ds[u] = p;
  35. }
  36.  
  37. int main(){
  38.  
  39.     dp[0] = 2;
  40.     for(int i = 1; i <= logN; ++i){
  41.         dp[i] = (lli)dp[i - 1] * dp[i - 1] % MOD;
  42.     }
  43.  
  44.     int nLion, Q;
  45.     scanf("%d%d", &nLion, &Q);
  46.     // DSU Setup
  47.     for(int i = 1; i <= nLion; ++i){
  48.         ds[i] = i;
  49.         sz[i] = 1;
  50.         isWith[i] = true;
  51.     }
  52.     int setCnt = nLion;
  53.     bool conflict = false;
  54.     while(Q--){
  55.         int u, v;
  56.         bool with;
  57.         scanf("%d%d%d", &with, &u, &v);
  58.         if(conflict){
  59.             cout << "0\n";
  60.             continue;
  61.         }
  62.         if(dsFind(u) == dsFind(v)){ // Check Confilct
  63.             if(isWith[u] == isWith[v] && !with || isWith[u] != isWith[v] && with){
  64.                 conflict = true;
  65.             }
  66.         } else { // Union
  67.             int headU = dsFind(u);
  68.             int headV = dsFind(v);
  69.             if(sz[headV] > sz[headU]){
  70.                 swap(headU, headV);
  71.                 swap(u, v);
  72.             }
  73.             ds[headV] = headU;
  74.             sz[headU] += sz[headV];
  75.             isWith[headV] = fusion(fusion(isWith[u], isWith[v]), with);
  76.             --setCnt;
  77.         }
  78.         if(conflict){
  79.             cout << "0\n";
  80.         } else {
  81.             cout << powerOf2(setCnt) << '\n';
  82.         }
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement