Advertisement
Guest User

Untitled

a guest
Jul 15th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <stack>
  5. #include <queue>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. struct Action {
  10.  
  11.     int *pos;
  12.     int prevval;
  13.  
  14.     inline Action(int *pos, int prevval) {
  15.         this->pos = pos;
  16.         this->prevval = prevval;
  17.     }
  18.  
  19.     inline void undo() {
  20.         *pos = prevval;
  21.     }
  22.  
  23. };
  24.  
  25. const int MAXN = 1111111, MAXQ = 2222222;
  26.  
  27. int type[MAXQ], v1[MAXQ], v2[MAXQ], comple[MAXQ];
  28. int *u[MAXQ];
  29. int k[MAXQ];
  30.  
  31. vector<int> graph[MAXN];
  32. int visited[MAXN], component[MAXN], size[MAXN];
  33. int counter = 1;
  34.    
  35. void dfs(int start, int v, stack<Action> &stack) {
  36.     visited[v] = counter;
  37.     stack.push(Action(&component[v], component[v]));
  38.     component[v] = start;
  39.     if (v != start) {
  40.         stack.push(Action(&size[start], size[start]));
  41.         size[start] += size[v];
  42.     }
  43.     int sz = graph[v].size();
  44.     for (int i = 0; i < sz; i++) {
  45.         int e = graph[v][i];
  46.         if (visited[e] != counter) {
  47.             dfs(start, e, stack);
  48.         }
  49.     }
  50. }
  51.    
  52. inline void addEdge(int from, int to) {
  53.     graph[from].push_back(to);
  54.     graph[to].push_back(from);
  55. }
  56.    
  57. inline void undoAll(stack<Action> &stack) {
  58.     while (stack.size() > 0) {
  59.         stack.top().undo();
  60.         stack.pop();
  61.     }
  62. }
  63.  
  64. int n, q;
  65.  
  66. stack<Action> stacks[25];
  67. queue<int> queues[25];
  68. int level = -1;
  69.    
  70. void decompose(int l, int r) {
  71.     level++;
  72.     /*
  73.     cout << l << " " << r << endl;
  74.     for (int i = 0; i < n; i++) {
  75.         cout << component[i] << " ";
  76.     }
  77.     cout << endl;
  78.     for (int i = 0; i < n; i++) {
  79.         cout << size[i] << " ";
  80.     }
  81.     cout << endl;
  82.     cout << endl;
  83.     */
  84.     if (l == r) {
  85.         if (type[l] == 3) {
  86.             vector<int> sorted(k[l]);
  87.             copy(u[l], u[l] + k[l], sorted.begin());
  88.             sort(sorted.begin(), sorted.end());
  89.             int sumsize = 0;
  90.             int len = sorted.size();
  91.             for (int i = 0; i < len; ) {
  92.                 int j = i;
  93.                 while (j < len && sorted[j] == sorted[i]) {
  94.                     j++;
  95.                 }
  96.                 sumsize += size[sorted[i]];
  97.                 i = j;
  98.             }
  99.             if (sumsize == len) {
  100.                 puts("YES");
  101.             } else {
  102.                 puts("NO");
  103.             }
  104.         }
  105.     } else {
  106.         int m = (l + r) / 2;
  107.         stack<Action> &stack = stacks[level];
  108.         queue<int> &q = queues[level];
  109.            
  110.         for (int i = m + 1; i <= r; i++) {
  111.             if (type[i] == 2 && comple[i] < l) {
  112.                 int from = v1[comple[i]], to = v2[comple[i]];
  113.                 addEdge(from, to);
  114.                 q.push(from);
  115.                 q.push(to);
  116.             }
  117.         }
  118.            
  119.         counter++;
  120.         while (q.size() > 0) {
  121.             int v = q.front();
  122.             q.pop();
  123.             if (visited[v] != counter) {
  124.                 dfs(v, v, stack);
  125.             }
  126.             graph[v].clear();
  127.         }
  128.            
  129.         for (int i = l; i <= m; i++) {
  130.             if (type[i] == 1) {
  131.                 if (v1[i] != component[v1[i]]) {
  132.                     stack.push(Action(&v1[i], v1[i]));
  133.                     v1[i] = component[v1[i]];
  134.                 }
  135.                 if (v2[i] != component[v2[i]]) {
  136.                     stack.push(Action(&v2[i], v2[i]));
  137.                     v2[i] = component[v2[i]];
  138.                 }
  139.             } else if (type[i] == 2) {
  140.                 int ii = comple[i];
  141.                 if (ii < l) {
  142.                     if (v1[ii] != component[v1[ii]]) {
  143.                         stack.push(Action(&v1[ii], v1[ii]));
  144.                         v1[ii] = component[v1[ii]];
  145.                     }
  146.                     if (v2[ii] != component[v2[ii]]) {
  147.                         stack.push(Action(&v2[ii], v2[ii]));
  148.                         v2[ii] = component[v2[ii]];
  149.                     }
  150.                 }
  151.             } else {
  152.                 int len = k[i];
  153.                 for (int j = 0; j < len; j++) {
  154.                     if (u[i][j] != component[u[i][j]]) {
  155.                         stack.push(Action(&u[i][j], u[i][j]));
  156.                         u[i][j] = component[u[i][j]];
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.            
  162.         decompose(l, m);
  163.            
  164.         undoAll(stack);
  165.            
  166.         for (int i = l; i <= m; i++) {
  167.             if (type[i] == 1 && comple[i] > r) {
  168.                 int from = v1[i], to = v2[i];
  169.                 addEdge(from, to);
  170.                 q.push(from);
  171.                 q.push(to);
  172.             }
  173.         }
  174.            
  175.         counter++;
  176.         while (q.size() > 0) {
  177.             int v = q.front();
  178.             q.pop();
  179.             if (visited[v] != counter) {
  180.                 dfs(v, v, stack);
  181.             }
  182.             graph[v].clear();
  183.         }
  184.            
  185.         for (int i = m + 1; i <= r; i++) {
  186.             if (type[i] == 1) {
  187.                 if (v1[i] != component[v1[i]]) {
  188.                     stack.push(Action(&v1[i], v1[i]));
  189.                     v1[i] = component[v1[i]];
  190.                 }
  191.                 if (v2[i] != component[v2[i]]) {
  192.                     stack.push(Action(&v2[i], v2[i]));
  193.                     v2[i] = component[v2[i]];
  194.                 }
  195.             } else if (type[i] == 2) {
  196.                 int ii = comple[i];
  197.                 if (ii <= m) {
  198.                     if (v1[ii] != component[v1[ii]]) {
  199.                         stack.push(Action(&v1[ii], v1[ii]));
  200.                         v1[ii] = component[v1[ii]];
  201.                     }
  202.                     if (v2[ii] != component[v2[ii]]) {
  203.                         stack.push(Action(&v2[ii], v2[ii]));
  204.                         v2[ii] = component[v2[ii]];
  205.                     }
  206.                 }
  207.             } else {
  208.                 int len = k[i];
  209.                 for (int j = 0; j < len; j++) {
  210.                     if (u[i][j] != component[u[i][j]]) {
  211.                         stack.push(Action(&u[i][j], u[i][j]));
  212.                         u[i][j] = component[u[i][j]];
  213.                     }
  214.                 }
  215.             }
  216.         }
  217.            
  218.         decompose(m + 1, r);
  219.            
  220.         undoAll(stack);
  221.     }
  222.     level--;
  223. }
  224.  
  225. int main() {
  226.     //freopen("input.txt", "r", stdin);
  227.     scanf("%d %d", &n, &q);
  228.        
  229.     for (int i = 0; i < n; i++) {
  230.         visited[i] = 0;
  231.         component[i] = i;
  232.         size[i] = 1;
  233.     }
  234.    
  235.     vector<int> addTime(q);
  236.     int addCnt = 0;
  237.        
  238.     for (int i = 0; i < q; i++) {
  239.         scanf("%d", &type[i]);
  240.         if (type[i] == 1) {
  241.             scanf("%d %d", &v1[i], &v2[i]);
  242.             v1[i]--;
  243.             v2[i]--;
  244.             comple[i] = q;
  245.             addTime[addCnt++] = i;
  246.         } else if (type[i] == 2) {
  247.             int m;
  248.             scanf("%d", &m);
  249.             m--;
  250.             comple[i] = addTime[m];
  251.             comple[addTime[m]] = i;
  252.         } else {
  253.             scanf("%d", &k[i]);
  254.             u[i] = new int[k[i]];
  255.             for (int j = 0; j < k[i]; j++) {
  256.                 int val;
  257.                 scanf("%d", &val);
  258.                 val--;
  259.                 u[i][j] = val;
  260.             }
  261.         }
  262.     }
  263.        
  264.     int logq = 0, qc = q;
  265.     while (qc > 1) {
  266.         qc = (qc + 1) / 2;
  267.         logq++;
  268.     }
  269.        
  270.     decompose(0, q - 1);
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement