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 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 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
 
本题目要求先建立图,顶点带权值;有两种操作:
   1.查询该分量中power最大的,若最大的不止一个,取下标小的;
   2.删除一条边
对于本题,若先建立并查集,再按操作顺序从前往后的话,不易处理,说以可以先读入边,在读入操作,当操作是删边时就删边。然后按删除后剩下的便建立并查集,从后往前进行,当遇到删边操作时就反过来添加边,从而保证每次询问都是当前正确的状态。
注意用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 a)
//查找a的父亲节点并压缩路径
{
	if(par[a]==a)
		return par[a];
	int pa=par[a];
	par[a]=Get_par(par[a]);
	return par[a];
}

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));
			//	g[a].erase(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]>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;
}

 

你可能感兴趣的:(数据结构,并查集)