Connections in Galaxy War(逆向并查集)

Connections in Galaxy War

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563

Time Limit: 3 Seconds      Memory Limit: 32768 KB

In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised when some of the stars wanted to seek help from the others.

In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star A directly or indirectly. In addition, this star should be more powerful than the star A. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes star A couldn't find such star for help.

Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.

Input

There are no more than 20 cases. Process to the end of file.

For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line contains N integers p0p1, ... , pn-1 (0 <= pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers ab (0 <= ab <= N - 1, a != b), which means star a and star b has a connection tunnel. It's guaranteed that each connection will only be described once.

In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line will be written in one of next two formats.

"destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a and star b was available before the monsters' attack.

"query a" - star a wanted to know which star it should turn to for help

There is a blank line between consecutive cases.

Output

For each query in the input, if there is no star that star a can turn to for help, then output "-1"; otherwise, output the serial number of the chosen star.

Print a blank line between consecutive cases.

Sample Input

2
10 20
1
0 1
5
query 0
query 1
destroy 0 1
query 0
query 1

Sample Output

1
-1
-1
-1

 总结自己被坑的两个点

1.输入边的时候要注意,应该把边从小到大排序或从大到小排序,不然可能会出现,加边的时候是1,2但是删边的时候是2,1的情况

2.这个应该不算坑点。。。是我自己没考虑到:要把不是将被删除的边先连起来。。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<string>
  6 #include<cstring>
  7 #include<queue>
  8 #include<vector>
  9 #include<map>
 10 #include<cmath>
 11 #include<stack>
 12 using namespace std;
 13 #define maxn 200005
 14 
 15 int fa[maxn],a[maxn];
 16 map<int,int>mp[maxn];
 17 
 18 
 19 void init(int n){
 20     for(int i=0;i<n+50;i++){
 21         fa[i]=i;
 22         a[i]=0;
 23         mp[i].clear();
 24     }
 25 }
 26 
 27 int Find(int x){
 28     int r=x,y;
 29     while(x!=fa[x]){
 30         x=fa[x];
 31     }
 32     while(r!=x){
 33         y=fa[r];
 34         fa[r]=x;
 35         r=y;
 36     }
 37     return x;
 38 }
 39 
 40 void join(int x,int y){
 41     int xx=Find(x);
 42     int yy=Find(y);
 43     if(xx!=yy){
 44         if(a[xx]==a[yy]){
 45             if(xx<yy) fa[yy]=xx;
 46             else fa[xx]=yy;
 47         }
 48         else{
 49             if(a[xx]>a[yy]) fa[yy]=xx;
 50             else fa[xx]=yy;
 51         }
 52     }
 53 }
 54 
 55 struct sair{
 56     char s[15];
 57     int x,y;
 58 }p[maxn];
 59 
 60 struct Line{
 61     int x,y;
 62 }L[maxn];
 63 
 64 int main(){
 65     int n;
 66     int kong=0;
 67     while(~scanf("%d",&n)){
 68         init(n);
 69 
 70         if(kong) printf("\n");
 71         for(int i=0;i<n;i++) scanf("%d",&a[i]);
 72         int m;
 73         scanf("%d",&m);
 74         for(int i=1;i<=m;i++){
 75             scanf("%d %d",&L[i].x,&L[i].y);
 76             if(L[i].x>L[i].y) swap(L[i].x,L[i].y);
 77         }
 78         int k;
 79         scanf("%d%*c",&k);
 80         for(int i=1;i<=k;i++){
 81             scanf("%s",&p[i].s);
 82             if(p[i].s[0]=='q') scanf("%d%*c",&p[i].x);
 83             else{
 84                 scanf("%d %d%*c",&p[i].x,&p[i].y);
 85                 if(p[i].x>p[i].y) swap(p[i].x,p[i].y);
 86                 mp[p[i].x][p[i].y]=1;
 87             }
 88         }
 89         int tmp;
 90         stack<int>st;
 91         for(int i=1;i<=m;i++){///容易忽略
 92             if(!mp[L[i].x][L[i].y]) join(L[i].x,L[i].y);
 93         }
 94         for(int i=k;i>=1;i--){
 95             if(p[i].s[0]=='q'){
 96                 tmp=Find(p[i].x);
 97                 if(a[tmp]>a[p[i].x]) st.push(tmp);
 98                 else st.push(-1);
 99             }
100             else{
101                 join(p[i].x,p[i].y);
102             }
103         }
104         while(!st.empty()){
105             printf("%d\n",st.top());
106             st.pop();
107         }
108         kong++;
109     }
110 
111 
112 }
View Code

 

posted on 2018-11-12 13:52  Fighting_sh  阅读(390)  评论(0编辑  收藏  举报

导航