Connections in Galaxy War

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 integerpi. When the starA wanted to seek help, it would send the message to the star with the largest power which was connected with starA directly or indirectly. In addition, this star should be more powerful than the starA. 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 starA 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 containsN integersp0,p1, ... ,pn-1 (0 <=pi <= 1000000000), representing the power of thei-th star. Then the third line is a single integerM (0 <=M <= 20000), that is the number of tunnels built before the war. ThenM lines follows. Each line has two integersa,b (0 <=a, b <= N - 1, a !=b), which means stara and starb 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 followingQ lines, each line will be written in one of next two formats.

  • "destroy a b" - the connection between star a and starb was destroyed by the monsters. It's guaranteed that the connection between stara and starb 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

题意:n个星球组成一个连接的网络,把星球看成点,通讯连线看成边相连,接下来有m个操作,有查询操作,添加边的操作和销毁边的操作,同时每个星球有一个权值,对一个点的查询操作返回的是与它间接或直接相连的星球取权值最大者的序号。

题解:这是一道并查集的题目,但是这道题有两个难点:1、如何删除路径2、如何找到并查集中权值最大的点

1.并查集只有添加边和查询两种操作,正难反易,考虑离线的做法,把所有数据保存下来,但建立集合的时候不把要删除的边加进去,然后逆序查询,每当遇到标记为删除的边时就连上,更新集合(星球大战之后的状态作为初始状态,进行并查集的操作,遇到destroy时就恢复a到b之间的通道

2.查找并查集中权值最大的点:我们在合并并查集的时候,将满足要求的权值最大的点放至根节点。这样,我们查找权值最大的点只需直接查找根节点即可

#include<iostream>
#include<algorithm>
#include<map>
#include<stdio.h>
#include<string.h>
#define N 10005
#define M 20005
#define Q 50005
using namespace std;
int set[N];
int ans[Q];
//星球 
struct node{
	int id;
	int power;
}star[N];
//隧道 
struct link{
	int x;
	int y;
}tunnel[M];
//查询 
struct ask{
	int x;
	int y;
	char s[10];
}query[Q];
//记录边
map<int, bool> mp;
//查找根节点
int find(int x){
	return set[x]==x?x:set[x]=find(set[x]);
//	while(set[x]!=x)
//		x=set[x];
//	return x;
} 
void merge(int a,int b){
	int ax=find(a);
	int by=find(b);
	if(ax!=by){
		//能量大的做根节点 
		if(star[ax].power>star[by].power)
			set[by]=ax;
		else if(star[ax].power<star[by].power)
			set[ax]=by;
		//在能量相同下,编号小的做跟节点 
		else{
			if(star[ax].id>star[by].id)
				 set[ax]=by;
			else
				set[by]=ax;
		}
	}
}
int main(){
	int n,m,t;
	int flag=1;
	while(cin>>n){
		mp.clear();
		for(int i=0;i<n;i++){
			scanf("%d",&star[i].power);
			star[i].id=i;
			set[i]=i;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			scanf("%d%d",&tunnel[i].x,&tunnel[i].y);
			//交换边更有助于判断 
			if(tunnel[i].x>tunnel[i].y)
				swap(tunnel[i].x,tunnel[i].y);
			标记没被毁掉的边 
			mp[tunnel[i].x*10000+tunnel[i].y]=false;
		}
		scanf("%d",&t);
		for(int i=0;i<t;i++){
			scanf("%s",query[i].s); 
			if(query[i].s[0]=='q')
				scanf("%d",&query[i].x);
			else{
				scanf("%d%d",&query[i].x,&query[i].y);
				//交换边更有助于判断 
				if(query[i].x>query[i].y)
					swap(query[i].x,query[i].y);
				//标记被毁掉的边 
				mp[query[i].x*10000+query[i].y]=true;
			}
		}
		//把没被毁掉的边合并起来
		for(int i=0;i<m;i++) 
			if(!mp[tunnel[i].x*10000+tunnel[i].y])
				merge(tunnel[i].x,tunnel[i].y);
		//逆向查找啦
		int sum=0;
		for(int i=t-1;i>=0;i--)
			if(query[i].s[0]=='q'){
				int k=find(query[i].x);
				//根节点能量较小 
				if(star[k].power>star[query[i].x].power)
					ans[sum++]=star[k].id;
				else
					ans[sum++]=-1;
			}
			//如果边被毁坏,则毁坏之前肯定联通,就加进去
		else
			merge(query[i].x,query[i].y);
		if(flag)
			flag=0;
		else
			printf("\n");
		//逆向输出 
		for(int i=sum-1;i>=0;i--)
			printf("%d\n", ans[i]);
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值