# bzoj1180: [CROATIAN2009]OTOCI

5
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5

## Sample Output

4
impossible
yes
6
yes
yes
15
yes
15
16

LCT模板题·····练手用的······
code：
 1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 using namespace std;
7 char ch; bool ok;
9     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
10     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
11     if (ok) x=-x;
12 }
13 const int maxn=100005;
14 int n,q,x,y;
15 char op[10];
16 struct LCT{
17     #define ls son[x][0]
18     #define rs son[x][1]
19     int fa[maxn],son[maxn][2],rev[maxn],val[maxn],sum[maxn];
20     int which(int x){return son[fa[x]][1]==x;}
21     bool isroot(int x){return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;}
22     void addtag_rev(int x){if (x) rev[x]^=1,swap(ls,rs);}
24     void relax(int x){
25         if (!isroot(x)) relax(fa[x]);
26         pushdown(x);
27     }
28     void updata(int x){sum[x]=sum[ls]+val[x]+sum[rs];}
29     void rotate(int x){
30         int y=fa[x],z=fa[y],d=which(x),dd=which(y);
31         fa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=fa[y];
32         if (!isroot(y)) son[z][dd]=x;
33         son[x][d^1]=y,fa[y]=x,updata(y);
34     }
35     void splay(int x){
36         relax(x);
37         while (!isroot(x)){
38             if (isroot(fa[x])) rotate(x);
39             else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x);
40             else rotate(x),rotate(x);
41         }
42         updata(x);
43     }
44     void access(int x){for (int p=0;x;x=fa[x]) splay(x),son[x][1]=p,p=x;}
46     void connect(int u,int v){make_root(u),fa[u]=v;}
47     int query_sum(int u,int v){make_root(u),access(v),splay(v);return sum[v];}
48     int find_left(int x){for (pushdown(x);son[x][0];x=son[x][0],pushdown(x));return x;}
49     bool query_con(int u,int v){make_root(u),access(v),splay(v);return find_left(v)==u;}
50     void modify(int x,int v){val[x]=sum[x]=v,splay(x);}
51 }T;
52 int main(){
57         if (op[1]=='b'){
58             if (T.query_con(x,y)) puts("no");
59             else T.connect(x,y),puts("yes");
60         }
61         else if (op[1]=='p') T.modify(x,y);
62         else if (op[1]=='e'){
63             if (T.query_con(x,y)) printf("%d\n",T.query_sum(x,y));
64             else puts("impossible");
65         }
66     }
67     return 0;
68 }

posted @ 2017-06-13 23:47  chenyushuo  阅读(202)  评论(0编辑  收藏  举报