2843: 极地旅行社

Description

不久之前,Mirko建立了一个旅行社,名叫“极地之梦”。这家旅行社在北极附近购买了N座冰岛,并且提供观光服
务。当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间。Mirko的旅行社遭受一次
重大打击,以至于观光游轮已经不划算了。旅行社将在冰岛之间建造大桥,并用观光巴士来运载游客。Mirko希望
开发一个电脑程序来管理这些大桥的建造过程,以免有不可预料的错误发生。这些冰岛从1到N标号。一开始时这些
岛屿没有大桥连接,并且所有岛上的帝企鹅数量都是知道的。每座岛上的企鹅数量虽然会有所改变,但是始终在[0
, 1000]之间。你的程序需要处理以下三种命令:
1."bridge A B"——在A与B之间建立一座大桥(A与B是不同的岛屿)。由于经费限制,这项命令被接受,当且仅当
A与B不联通。若这项命令被接受,你的程序需要输出"yes",之
后会建造这座大桥。否则,你的程序需要输出"no"。
2."penguins A X"——根据可靠消息,岛屿A此时的帝企鹅数量变为X。这项命令只是用来提供信息的,你的程序不
需要回应。
3."excursion A B"——一个旅行团希望从A出发到B。若A与B连通,你的程序需要输出这个旅行团一路上所能看到的
帝企鹅数量(包括起点A与终点B),若不联通,你的程序需要输出"impossible"。

Input

第一行一个正整数N,表示冰岛的数量。
第二行N个范围[0, 1000]的整数,为每座岛屿初始的帝企鹅数量。
第三行一个正整数M,表示命令的数量。接下来M行即命令,为题目描述所示。
1<=N<=30000,1<=M<=100000

Output

对于每个bridge命令与excursion命令,输出一行,为题目描述所示。

Sample Input

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保存值,并查集保存联通性。
启发式合并也是可以的。
  1 #include<iostream>
  2 #include<cstdlib>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<algorithm>
  7 #include<string>
  8 #include<map>
  9 #include<queue>
 10 #include<vector>
 11 #include<set>
 12 #define inf 1000000000
 13 #define maxn 30000+5
 14 #define maxm 10000+5
 15 #define eps 1e-10
 16 #define ll long long
 17 #define for0(i,n) for(int i=0;i<=(n);i++)
 18 #define for1(i,n) for(int i=1;i<=(n);i++)
 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
 22 using namespace std;
 23 int read(){
 24     int x=0,f=1;char ch=getchar();
 25     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 26     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 27     return x*f;
 28 }
 29 int n,m,c[maxn][2],fa[maxn],v[maxn],sum[maxn],sta[maxn],f[maxn];
 30 bool rev[maxn];
 31 bool isroot(int x){
 32     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
 33 }
 34 void pushup(int x){
 35     sum[x]=sum[c[x][1]]+sum[c[x][0]]+v[x];
 36 }
 37 void rever(int x){
 38     rev[x]^=1;
 39     swap(c[x][1],c[x][0]);
 40 }
 41 void pushdown(int x){
 42     if(!rev[x])return ;
 43     rever(c[x][1]);rever(c[x][0]);
 44     rev[x]=0;
 45 }
 46 void rotate(int x){
 47     int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
 48     if(!isroot(y))c[z][c[z][1]==y]=x;
 49     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
 50     c[y][l]=c[x][r];c[x][r]=y;
 51     pushup(y);pushup(x);
 52 }
 53 void splay(int x){
 54     int top=0;sta[++top]=x;
 55     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
 56     for(;top;)pushdown(sta[top--]);
 57     while(!isroot(x)){
 58         int y=fa[x],z=fa[y];
 59         if(!isroot(y)){
 60             if(c[z][0]==y^c[y][0]==x)
 61                 rotate(x);
 62             else rotate(y);
 63         }
 64         rotate(x);
 65     }
 66 }
 67 void access(int x){
 68     for(int y=0;x;x=fa[x]){
 69         splay(x);c[x][1]=y;pushup(x);y=x;
 70     }
 71 }
 72 void makeroot(int x){
 73     access(x);splay(x);rever(x);
 74 }
 75 void split(int x,int y){
 76     makeroot(x);access(y);splay(y);
 77 }
 78 int findroot(int x){
 79     return f[x]==x?x:f[x]=findroot(f[x]);
 80 }
 81 void link(int x,int y){
 82     if(findroot(x)==findroot(y)){
 83         printf("no\n");
 84         return ;
 85     }
 86     makeroot(x);fa[x]=y;splay(x);f[findroot(x)]=findroot(y);
 87     printf("yes\n");
 88 }
 89 void query(int x,int y){
 90     if(findroot(x)==findroot(y)){
 91         split(x,y),printf("%d\n",sum[y]);
 92         return ;
 93     }
 94     printf("impossible\n");
 95 }
 96 int main(){
 97     //freopen("input.txt","r",stdin);
 98     //freopen("output.txt","w",stdout);
 99     n=read();
100     for1(i,n)v[i]=sum[i]=read(),f[i]=i;
101     m=read();
102     while(m--){
103         char ch=getchar();
104         while(ch!='e'&&ch!='b'&&ch!='p')ch=getchar();
105         int x=read(),y=read();
106         if(ch=='e')query(x,y);
107         else if(ch=='b')link(x,y);
108         else makeroot(x),v[x]=y,pushup(x);
109     }
110     return 0;
111 }
View Code

 

posted @ 2016-07-11 10:16  HTWX  阅读(122)  评论(0编辑  收藏  举报