bzoj2049: [Sdoi2008]Cave 洞穴勘测
lct入门题?
得换根了吧TAT
这大概不是很成熟的版本..
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<cstdio> 6 using namespace std; 7 8 const int Maxn = 10010; 9 10 int ch[Maxn][2],p[Maxn],flip[Maxn]; 11 int n,m; 12 13 const int D=15000000; 14 char in[D],out[300010*10],*I=in,*O=out; 15 #define gc (*I++) 16 #define pc(x) ((*O++)=x) 17 template <typename Q> 18 void gt(Q&x) { 19 static char c,f; 20 for(c=gc,f=0;!isdigit(c);c=gc)if(c=='-') f=1; 21 for(x=0;isdigit(c);c=gc) x=x*10+c-'0'; 22 f && (x=-x); 23 } 24 25 template <typename Q> 26 void pt(Q x){ 27 static char stk[20]; 28 static int top; 29 top=0; 30 if(x==0) pc('0'); 31 for(;x;x/=10) stk[++top] = x%10+'0'; 32 for(;top;top--) pc(stk[top]); 33 } 34 35 void down(int x){ 36 if(flip[x]) { 37 swap(ch[x][0],ch[x][1]); 38 flip[ch[x][0]]^=1; 39 flip[ch[x][1]]^=1; 40 flip[x]=0; 41 } 42 } 43 44 bool isroot(int x){ 45 return ch[p[x]][0]!=x && ch[p[x]][1]!=x; 46 } 47 48 void rotate(int x) { 49 int y=p[x],z=p[y]; 50 int l=ch[y][1]==x,r=l^1; 51 if(!isroot(y)) ch[z][ch[z][1]==y]=x; 52 p[ch[x][r]]=y; 53 p[y]=x; 54 p[x]=z; 55 56 ch[y][l]=ch[x][r]; 57 ch[x][r]=y; 58 } 59 60 int stk[Maxn],top; 61 void splay(int x){ 62 stk[top=1]=x; 63 for(int t=x;!isroot(t);t=p[t]) stk[++top]=p[t]; 64 for(;top;top--) down(stk[top]); 65 66 for(;!isroot(x);){ 67 int y=p[x],z=p[y]; 68 if(!isroot(y)){ 69 if( (ch[y][1]==x)^(ch[z][1]==y) ) 70 rotate(x);else rotate(y); 71 } 72 rotate(x); 73 } 74 } 75 76 void access(int x){ 77 for(int t=0;x;x=p[t=x]){ 78 splay(x); 79 ch[x][1]=t; 80 } 81 } 82 83 int getroot(int x){ 84 for(access(x),splay(x);ch[x][0];x=ch[x][0]); 85 return x; 86 } 87 88 void newroot(int x) { 89 access(x); 90 splay(x); 91 flip[x]^=1; 92 } 93 94 void cut(int x){ 95 access(x); 96 splay(x); 97 p[ch[x][0]]=0; 98 ch[x][0]=0; 99 } 100 101 void combine(int x,int y) { 102 newroot(x); 103 p[x]=y; 104 } 105 106 void work() { 107 gt(n),gt(m); 108 char c; 109 int a,b; 110 for(int i=1;i<=m;i++) { 111 for(;c=gc,c!='Q' && c!='C' &&c!='D';); 112 gt(a),gt(b); 113 if(c=='Q') printf("%s\n",getroot(a)==getroot(b)?"Yes" : "No"); 114 else if(c=='C') combine(a,b); 115 else newroot(a),cut(b); 116 } 117 } 118 119 int main() { 120 #ifdef DEBUG 121 freopen("in.txt","r",stdin); 122 freopen("out.txt","w",stdout); 123 #endif 124 fread(in,1,D,stdin); 125 126 work(); 127 128 return printf(out),0; 129 }
原文出处http://www.cnblogs.com/showson/