Advertisement
minh141198

Untitled

Oct 31st, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <bitset>
  6. #include <vector>
  7. #include <string>
  8. #include <cmath>
  9. #include <map>
  10. using namespace std;
  11. typedef pair<int, int> ii;
  12. #define X first
  13. #define Y second
  14. #define PB push_back
  15. #define REP(i, n) for(int i=0; i<(int) (n); i++)
  16.  
  17. const int N = 110000;
  18.  
  19.  
  20. int par[N], rank[N];
  21.  
  22. bitset<N> hasP, hasR;
  23. vector<ii> chgP, chgR;
  24.  
  25. struct dsu{
  26.     bool backup;
  27.    
  28.     void init(int n){
  29.         for(int i=1; i<=n; i++){
  30.             par[i] = i;
  31.             rank[i] = 0;
  32.         }
  33.         backup = 0;
  34.     }
  35.  
  36.     void restore(){
  37.         REP(i, chgP.size()){
  38.             par[chgP[i].X] = chgP[i].Y;
  39.             hasP[chgP[i].X] = 0;
  40.         }
  41.         chgP.clear();
  42.         REP(i, chgR.size()){
  43.             rank[chgR[i].X] = chgR[i].Y;
  44.             hasR[chgR[i].X] = 0;
  45.         }
  46.         chgR.clear();
  47.         backup = 0;
  48.     }
  49.  
  50.     int find(int u){
  51.         if(par[u] == u) return u;
  52.         if(backup) return find(par[u]);
  53.         return par[u] = find(par[u]);
  54.     }
  55.    
  56.     void saveP(int u){
  57.         if(backup && hasP[u] == 0){
  58.             hasP[u] = 1;
  59.             chgP.PB(ii(u, par[u]));
  60.         }
  61.     }
  62.  
  63.     void join(int u, int v){
  64.         u = find(u);
  65.         v = find(v);
  66.         if(u != v){
  67.             if(rank[u] < rank[v]){
  68.                 saveP(u);
  69.                 par[u] = v;
  70.             } else if(rank[u] > rank[v]){
  71.                 saveP(v);
  72.                 par[v] = u;
  73.             } else {
  74.                 saveP(u);
  75.                 if(backup && hasR[v] == 0){
  76.                     hasR[v] = 1;
  77.                     chgR.PB(ii(v, rank[v]));
  78.                 }
  79.                 par[u] = v;
  80.                 rank[v]++;
  81.             }
  82.         }
  83.     }
  84. };
  85.  
  86. dsu D;
  87. int n, Q, b, next[N], cmd[N];
  88. ii E[N];
  89. map<ii, int> mp;
  90. map<ii, pair<int, vector<int> > > ma;
  91.  
  92.  
  93. void MAIN(){
  94.     cin >> n >> Q;
  95.     memset(next, 0xff, sizeof next);
  96.     for(int i=0; i<Q; i++){
  97.         cin >> cmd[i] >> E[i].X >> E[i].Y;
  98.         if(E[i].X > E[i].Y) swap(E[i].X, E[i].Y);
  99.         if(cmd[i] == 1){
  100.             ma[E[i]].Y.PB(i);
  101.             ma[E[i]].X++;
  102.             next[i] = Q;
  103.         }
  104.         else if(cmd[i] == 2){
  105.             map<ii, pair<int, vector<int> > >::iterator it = ma.find(E[i]);
  106.             if(it == ma.end()) continue;
  107.             (it->Y.X)--;
  108.             if(it->Y.X == 0){
  109.                 REP(j, (it->Y.Y).size()) next[it->Y.Y[j]] = i-1;
  110.                 ma.erase(it);
  111.             }
  112.         }
  113.     }
  114.     b = sqrt(Q);
  115.     for(int i=0; i<Q; i+=b){
  116.         int en = i+b-1;
  117.         vector<int> v[400];
  118.         if(en >= Q) en = Q-1;
  119.         D.init(n);
  120.         REP(j, i) if(next[j] > en) D.join(E[j].X, E[j].Y);
  121.         else if(next[j] >= i) v[next[j]-i].PB(j);
  122.        
  123.         string s;
  124.        
  125.         for(int j=en; j>=i; j--){
  126.             REP(l, v[j-i].size()) D.join(E[v[j-i][l]].X, E[v[j-i][l]].Y);
  127.             if(cmd[j] == 3){
  128.                 D.backup = 1;
  129.                 for(int l=i; l<j; l++) if(next[l] >= j) D.join(E[l].X, E[l].Y);
  130.                 if(D.find(E[j].X) == D.find(E[j].Y) ) s += '1';
  131.                 else s += '0';
  132.                 D.restore();
  133.             }
  134.         }
  135.         reverse(s.begin(), s.end()); cout << s;
  136.     }
  137. }
  138.  
  139. int main(){
  140.     //freopen("input.in", "r", stdin);
  141.     ios::sync_with_stdio(false);
  142.     MAIN();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement