BZOJ 2049 LCT

    Orz 黄学长, 当然也要感谢自己(这次打的比较顺利,虽然题目操作很少,也比较简单),加油, 相信自己, 会更强大的, 但还是要虚心, 才会有更大的进步。 不过还是有点不懂,找个时间,问一下czl吧!

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #define rep(i,j,k) for(int i = j; i <= k; i++)
  5 #define lc c[k][0]
  6 #define rc c[k][1]
  7 #define maxn 10233
  8 using namespace std;
  9 
 10 int n, m;
 11 int read()
 12 {
 13     int s = 0, t = 1; char c = getchar();
 14     while( !isdigit(c) ){
 15         if( c == '-' ) t = -1; c = getchar();
 16     }
 17     while( isdigit(c) ){
 18         s = s * 10 + c - '0'; c = getchar();
 19     }
 20     return s * t;
 21 }
 22 
 23 int c[maxn][2], pa[maxn];
 24 bool rev[maxn];
 25 int q[maxn];
 26 
 27 inline bool root(int x)
 28 {
 29     int k = pa[x];
 30     return lc != x && rc != x; 
 31 }
 32 
 33 void pushdown(int k)
 34 {
 35     if( rev[k] ){
 36         rev[lc] ^= 1, rev[rc] ^= 1;
 37         swap(lc,rc);
 38         rev[k] ^= 1;
 39     }
 40 } 
 41 
 42 void rorate(int k)
 43 {
 44     int fa = pa[k], gfa = pa[fa];
 45     int l = c[fa][1] == k, r = l ^ 1;
 46     if( !root(fa) ){
 47         c[gfa][c[gfa][1] == fa] = k;
 48     }    
 49     pa[k] = gfa, pa[c[k][r]] = fa, pa[fa] = k;
 50     c[fa][l] = c[k][r]; c[k][r] = fa; 
 51 }
 52 
 53 void splay(int k)
 54 {
 55     int top = 0;
 56     q[++top] = k;
 57     for( int x = k; !root(x); x = pa[x] ){
 58         q[++top] = pa[x];
 59     }
 60     while( top ) pushdown(q[top--]);
 61     while( !root(k) ){
 62         int fa = pa[k], gfa = pa[fa];
 63         if( !root(fa) ){
 64             if( c[fa][0] == k ^ c[gfa][0] == fa ) rorate(k);
 65             else rorate(fa);
 66         }
 67         rorate(k);
 68     } 
 69 }
 70 
 71 void access(int x)
 72 {
 73     for(int t = 0; x; t = x, x = pa[x]){
 74         splay(x), c[x][1] = t;
 75     }
 76 }
 77 
 78 void makeroot(int x)
 79 {
 80     access(x), splay(x); rev[x] ^= 1;
 81 }
 82 
 83 void link(int x,int y)
 84 {
 85     makeroot(x); pa[x] = y; splay(x);
 86 }
 87 
 88 void cut(int x,int y)
 89 {
 90     makeroot(x); access(y), splay(y); c[y][0] = pa[x] = 0;
 91 }
 92 
 93 int find(int k)
 94 {
 95     access(k); splay(k);
 96     while( lc ) k = lc;
 97     return k; 
 98 }
 99 
100 bool solve(int x,int y)
101 {
102     if( find(x) == find(y) ) return 1;
103     else return 0;
104 }
105 
106 int main()
107 {
108     n = read(), m = read(); char c[10];
109     rep(i,1,m){
110         scanf("%s", c);
111         int x = read(), y = read();
112         if( c[0] == 'C' ){
113             link(x,y);
114         }
115         if( c[0] == 'Q' ){
116             if( solve(x,y) ) puts("Yes");
117             else puts("No");
118         }
119         if( c[0] == 'D' ){
120             cut(x,y);
121         }
122     }
123     return 0;
124 }

 

posted on 2016-01-08 18:38  83131  阅读(182)  评论(0编辑  收藏  举报

导航