zoj3261(并查集+set) (逆向)

Connections in Galaxy War

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 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条双向路径

然后是k个询问  query a 为查找a可以向谁求救,求救的目标要求为力量比a打,且直接或间接有通道连接,不行能话输出-1

destory a b  摧毁a和b之间的通道

思路:可以注意用set容器存储边,这样可以缩短插入、删除、查找的时间复杂度,应为这些都是set的基本操作,方便求复杂度为O(logN)。

同时对建立并查集这题可以逆向,因为一开始就建立的话摧毁要删除并查集非常的不方便,可以先进行保存要摧毁的边,然后从最后一个开始判断,遇到摧毁就将这条边通过并查集合并,合并比删除方便很多

注意:这题要求每两个例子之间要有空行 最后结束不能有多余的空行

#include<iostream>
#include<cstdio>
#include<set>
using  namespace std;

const int MAX=10000+100;
int par[MAX];
int power[MAX];
set<int>g[MAX];
int query[50000+100][2];
int ans[50000+100];

int Get_par(int x)
//查找a的父亲节点并压缩路径
{

	int r=x;
	while(r!=par[r])
		r=par[r];
	int i=x,j;
	while(i!=r)
	{
		j=par[i];
		par[i]=r;
		i=j;
	}
	return r;
}

void Merge(int a,int b)
//合并a,b
{
	int pa,pb;
	pa=Get_par(a);
	pb=Get_par(b);
	if(power[pa]<power[pb])//力量大的作为根节点
	{
		par[pa]=pb;
	}
	else if(power[pa]>power[pb])
	{
		par[pb]=pa;
	}
	else
	{
		if(pa<pb)//如果力量  则编号小的作为根节点
			par[pb]=pa;
		else par[pa]=pb;
	}
}

int main()
{
	int n,m,i,a,b,qnum,tag=0;
	char cmd[10];
	while(~scanf("%d",&n))
	{
		if(tag++)			//连续的例子之间输出一个空行
			printf("\n");
		for(i=0;i<MAX;i++)//初始化  对每个集合清空
		{
			par[i]=i;
			g[i].clear();
		}

		for(i=0;i<n;i++)
		{
			scanf("%d",&power[i]);
		}

		scanf("%d",&m);
		for(i=0;i<m;i++)//防止重复
		{
			scanf("%d%d",&a,&b);
			if(a>b)
				swap(a,b);
			g[a].insert(b);
		}

		scanf("%d",&qnum);
		for(i=0;i<qnum;i++)
		{
			scanf("%s",cmd);
			if(cmd[0]=='d')
			{
				scanf("%d%d",&a,&b);
				if(a>b)
					swap(a,b);
				query[i][0]=a,query[i][1]=b;
				g[a].erase(g[a].find(b));//查找b在a集合的位置  然后删除b
			}
			else
			{
				scanf("%d",&a);
				query[i][0]=a,query[i][1]=-1;
			}
		}

		for(i=0;i<n;i++)
        {
            for(set<int>::iterator it=g[i].begin();it!=g[i].end();it++)//建立并查集
                Merge(i,*it);
        }

		int id=0;
		for(i=qnum-1;i>=0;i--)//从最后一个开始判断
		{
			if(query[i][1]==-1)		//进行查询操作
			{
				int tmp=Get_par(query[i][0]);//返回根节点
				if(power[tmp]>power[query[i][0]])//如果力量更大的话 则满足题意
					ans[id++]=tmp;
				else ans[id++]=-1;//否则不成立
			}
			else						//摧毁操作  逆向返回 则变为使其合并
			{
				Merge(query[i][0],query[i][1]);
			}
		}

		for(i=id-1;i>=0;i--)
			printf("%d\n",ans[i]);
	}
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值