HDU - 3635 Dragon Balls (并查集)

                                           Dragon Balls

Problem Description

Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together.


His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.

Input

The first line of the input is a single positive integer T(0 < T <= 100).
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
  T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
  Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)

Output

For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.

Sample Input

2

3 3

T 1 2

T 3 2

Q 2

3 4

T 1 2

Q 1

T 1 3

Q 1

Sample Output

Case 1:

2 3 0

Case 2:

2 2 1

3 3 2

题意描述:

有n个龙珠散落在n个城市,编号1-n;有m组行动,‘T’为将城市a的龙珠运输到城市b中,‘Q’为询问编号为x的龙珠现在在哪个城市,这个城市现在有多少个龙珠,编号为x的龙珠被运输了多少次。输出每次询问的结果。

解题思路:
定义一个结构体,f[i].x为存储龙珠编号为i的龙族运输城市的父亲,f[i].y为存储i号城市现在共有多少龙珠,f[i].s存储编号为i的龙珠被运输了多少次。当被运输后,城市的龙珠数等于原有龙珠数加上被运输的龙珠的祖先城市的龙珠数,在每次查找祖先时,龙珠的被运输次数龙珠祖先的运输次数等于龙珠祖先的运输次数加上龙珠本身被运输的次数。在输出所在城市有多少龙珠时,也能进行查找与多少龙珠和所查龙珠祖先相同,不过会时间超限。(说的有点乱了,粘了个别人的题解https://blog.csdn.net/niushuai666/article/details/6990421

#include<stdio.h>
#include<string.h>
struct A
{
	int x;
	int y;
	int s;
}f[10010];
int getf(int i)
{
	int t;
	if(f[i].x==i)
		return i;
	else
	{
		t=f[i].x;
		f[i].x=getf(f[i].x);
		f[i].s=f[t].s+f[i].s;
		return f[i].x;
	}
}
void merge(int u,int v)
{
	int t1,t2;
	t1=getf(u);
	t2=getf(v);
	if(t1!=t2)
	{
		f[t1].x=t2;
		f[t2].y=f[t2].y+f[t1].y;
		f[t1].s++;
		f[t1].y=0;
	}
}
int main()
{
	int t,n,m,a,b,x,i,cut,j;
	char q;
	while(scanf("%d",&t)!=EOF)
	{
		cut=0;
		while(t--)
		{
			cut++;
			scanf("%d%d",&n,&m);
			printf("Case %d:\n",cut);
			
			for(i=1;i<=n;i++)
			{
				f[i].x=i;
				f[i].y=1;
				f[i].s=0;
			}
			getchar();
			for(i=1;i<=m;i++)
			{
				scanf("%c",&q);
				if(q=='T')
				{
					scanf("%d%d",&a,&b);
					merge(a,b); 
					getchar();
				}
				else if(q=='Q')
				{
					scanf("%d",&x);
					getchar();
					a=getf(x);
					printf("%d %d %d\n",a,f[a].y,f[x].s);
				}
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/kongsanjin/article/details/81482634
今日推荐