Hduoj3635【带权并查集】

/*Dragon Balls 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 0   Accepted Submission(s) : 0
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 

Author
possessor WC

Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU*/
#include<stdio.h>
#include<string.h>
int n, m, f[10010], trans[10010], nums[10010];
int find(int x)
{
	if(x != f[x])
	{
		int temp = find(f[x]);
		trans[x] += trans[f[x]];
		f[x] = temp;
	}
	return f[x];
}
void merge(int x, int y)
{
	x = find(x);
	y = find(y);
	if(x != y)
	{
		trans[x] = 1;
		nums[y] += nums[x];
		nums[x] = 0;
		f[x] = y;
	}
}
int main()
{
	int i, j, k, t;
	scanf("%d", &t);
	i = 1;
	while(t--)
	{
		printf("Case %d:\n", i++);
		scanf("%d%d", &n, &m);
		for(j = 1; j <= n; ++j)
		{
			f[j] = j;
			nums[j] = 1;
		}
		memset(trans, 0, sizeof(trans));
		while(m--)
		{
			getchar();
			char ch;
			scanf("%c", &ch);
			if(ch == 'T')
			{
				int a, b;
				scanf("%d%d", &a, &b); 
				merge(a, b);
			}
			else
			{
				int c;
				scanf("%d", &c);
				k = find(c);
				printf("%d %d %d\n", k, nums[k], trans[c]);
			}
		}
	}
	return 0;
}


题意:刚开始每个city都有一个对应编号的球,现在给出2个命令,一个是T A B 将A所在的城市里的所有球转移到B球所在的城市, 一个是C A 要求输出A球所在的城市的编号和该城市共有多少个球,以及A球被转移的次数。

思路:首先这里刚开始球的编号和城市的编号一一对应,要想到并查集的方法,其次对于T命令的操作就是合并A和B的城市,只要将统计城市球数的数组更新一下即可,并对A城市的根节点所在的球的转移次数设置为1 ,因为显然这个根节点所对应的球是第一次被转移,难点在于其他的球的转移次数需要根据这个根节点的转移次数来求出。关键就是路径压缩了,要想求某个球的转移次数,需要对它进行路径的更新,一直更新到它的根节点,根据根节点的转移次数为1,回溯递推出要求的球的转移次数。

难点:要理清球的转移次数在路径压缩时的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值