bzoj1180 tree

题目链接

link cut tree

模板题

link cut tree不都是模板题嘛?(雾

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<string>
  7 #include<cmath>
  8 #include<ctime>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<set>
 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
 14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
 15 #define Clear(a,b) memset(a,b,sizeof(a))
 16 #define inout(x) printf("%d",(x))
 17 #define douin(x) scanf("%lf",&x)
 18 #define strin(x) scanf("%s",(x))
 19 #define LLin(x) scanf("%lld",&x)
 20 #define op operator
 21 #define CSC main
 22 typedef unsigned long long ULL;
 23 typedef const int cint;
 24 typedef long long LL;
 25 using namespace std;
 26 void inin(int &ret)
 27 {
 28     ret=0;int f=0;char ch=getchar();
 29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
 30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
 31     ret=f?-ret:ret;
 32 }
 33 int n,q,w[30030],ch[30030][2],fa[30030],sum[30030],rev[30030];
 34 bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
 35 void maintain(int x){if(x)sum[x]=w[x]+sum[ch[x][0]]+sum[ch[x][1]];}
 36 void rotate(int x)
 37 {
 38     int y=fa[x],z=fa[y];
 39     if(!isroot(y))ch[z][ch[z][1]==y]=x;
 40     fa[x]=z,fa[y]=x;
 41     int d=ch[y][1]==x;
 42     fa[ch[x][d^1]]=y;
 43     ch[y][d]=ch[x][d^1];
 44     ch[x][d^1]=y;
 45     maintain(y);maintain(x);
 46 }
 47 void down(int x)
 48 {
 49     if(rev[x])
 50     {
 51         swap(ch[x][0],ch[x][1]);
 52         rev[ch[x][0]]^=1;
 53         rev[ch[x][1]]^=1;
 54         rev[x]=0;
 55     }
 56 }
 57 int sta[30030],top;
 58 void splay(int x)
 59 {
 60     top=0;int xx=x;sta[++top]=xx;
 61     while(!isroot(xx))sta[++top]=fa[xx],xx=fa[xx];
 62     while(top)down(sta[top--]);
 63     while(!isroot(x))
 64     {
 65         int y=fa[x],z=fa[y];
 66         if(!isroot(y))
 67             if((ch[y][1]==x)^(ch[z][1]==y))rotate(x);
 68             else rotate(y);else ;
 69         rotate(x);
 70     }
 71 }
 72 void access(int x)
 73 {
 74     int temp=0;
 75     while(x)
 76     {
 77         splay(x);
 78         ch[x][1]=temp;maintain(x);
 79         temp=x,x=fa[x];
 80     }
 81 }
 82 void reverse(int x)
 83 {
 84     access(x),splay(x),rev[x]^=1;
 85 }
 86 void link(int x,int y)
 87 {
 88     reverse(x),fa[x]=y;
 89 }
 90 void cut(int x,int y)
 91 {
 92     reverse(x),access(y),fa[x]=ch[y][0]=0;maintain(y);
 93 }
 94 int find(int x)
 95 {
 96     access(x);splay(x);
 97     while(ch[x][0])x=ch[x][0];
 98     return x;
 99 }
100 int main()
101 {
102     inin(n);char ss[22];
103     re(i,1,n)inin(w[i]),sum[i]=w[i];
104     inin(q);
105     re(i,1,q)
106     {
107         strin(ss);int a,b;inin(a),inin(b);
108         if(ss[0]=='b')
109         {
110             if(find(a)==find(b))cout<<"no\n";
111             else 
112             {
113                 cout<<"yes\n";
114                 link(a,b);
115             }
116         }
117         else if(ss[0]=='p')
118         {
119             splay(a),w[a]=b,maintain(a);
120         }
121         else if(ss[0]=='e')
122         {
123             if(find(a)!=find(b)){cout<<"impossible\n";continue;}
124             reverse(a),access(b),splay(b);
125             printf("%d\n",sum[b]);
126         }
127     }
128      return 0;
129 }

 

你可能感兴趣的:(bzoj1180 tree)