Description
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 p0, p1, ... , 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 a, b (0 <= a, b <= 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
解题思路:简单的并查集,另加一些限制条件:帮助者的能量要大于寻求者,并且是最大,再有帮助者能量不止一个,就寻求编号小的,记得无向边双向标记,反向离线处理答案即可。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn=10005; 5 const int maxq=50005; 6 int n,m,q,a,b,f=0,fa[maxn],eng[maxn],ans[maxq],tmp[maxq]; 7 char str[10];bool flag[maxq]; 8 map<pair<int,int>,bool> mp1; 9 map<int,pair<int,int> > mp2,mp3; 10 void init(){ 11 for(int i=0;i<n;++i)fa[i]=i; 12 memset(ans,0,sizeof(ans)); 13 memset(flag,false,sizeof(flag)); 14 memset(tmp,0,sizeof(tmp)); 15 mp1.clear(),mp2.clear(),mp3.clear(); 16 } 17 int find_fa(int x){ 18 return fa[x]==x?x:fa[x]=find_fa(fa[x]); 19 } 20 void unite(int x,int y){ 21 x=find_fa(x),y=find_fa(y); 22 if(x!=y){ 23 if(eng[x]>eng[y])fa[y]=x; 24 else if(eng[x]<eng[y])fa[x]=y; 25 else if(x<y)fa[y]=x; 26 else fa[x]=y; 27 } 28 } 29 inline int read(){ 30 int x=0,f=1;char ch=getchar(); 31 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 32 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 33 return x*f; 34 } 35 inline void print(int x){ 36 if(x<0)putchar(‘-‘),x=-x; 37 if(x>9)print(x/10); 38 putchar(x%10+‘0‘); 39 } 40 int main(){ 41 while(~scanf("%d",&n)){ 42 init(); 43 if(f++)puts(""); 44 for(int i=0;i<n;++i)eng[i]=read(); 45 m=read(); 46 for(int i=0;i<m;++i){ 47 a=read(),b=read(); 48 if(a>b)swap(a,b);///规定方向避免重复,相当于建双向边 49 mp2[i]=make_pair(a,b); 50 } 51 q=read(); 52 for(int i=0;i<q;++i){ 53 scanf("%s",str); 54 if(!strcmp(str,"query"))tmp[i]=read();///查询 55 else{ 56 a=read(),b=read();flag[i]=true; 57 if(a>b)swap(a,b); 58 mp1[mp3[i]=make_pair(a,b)]=true; 59 } 60 } 61 for(int i=0;i<m;++i) 62 if(!mp1[mp2[i]])unite(mp2[i].first,mp2[i].second); 63 for(int i=q-1;i>=0;--i){///反向离线处理答案 64 if(flag[i])unite(mp3[i].first,mp3[i].second); 65 else{ 66 int xx=find_fa(tmp[i]); 67 ans[i]=eng[xx]>eng[tmp[i]]?xx:-1;///祖先的能量严格大于其本身,同时包含不能得到帮助的情况 68 } 69 } 70 for(int i=0;i<q;++i) 71 if(!flag[i])print(ans[i]),puts(""); 72 } 73 return 0; 74 }
原文地址:https://www.cnblogs.com/acgoto/p/10080372.html