hdu 3635 Dragon Balls (并查集,路径压缩应用)

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4465    Accepted Submission(s): 1706


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.
hdu 3635 Dragon Balls (并查集,路径压缩应用)_第1张图片
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
 

题目中遇到的问题:

如何储存龙珠的转移次数。 因为龙珠从一个城市到另一个城市,开始想到用数组存,但是一旦城市转移,下标就改变了,然后想到结构体数组,结构体里设置一个positon,记录当前位置,但是城市数量最多能达到10000,因此查询操作将非常耗时间。

后来得到思路。

龙珠 从一个城市转移到另一个城市, 然后这个城市的所有龙珠(看做集合E),又转移到另一个城市(将转移后的整体看做集合F)。。。

因此,一个龙珠的转移次数就等于本身转移的那一次(如果转移过),再加上龙珠第一次转移到的”城市“,也就是集合E转移的次数,而集合E的转移次数又需要加上集合F的赚转移次数。。。

以此类推 直到我们找到最终那个城市(题目假设的是龙珠would be transported to other cities,所以不会出现环)

并查集的查询操作有个 叫做路径压缩的优化方法,就是将原来的子节点全部接到根节点上。

它有两个实现方法,一个迭代版,一个递归版。

这里用递归版本的正好可以完成这个问题;

int Find(int x){
	if(x==father[x])
		return x;
	int tmp = father[x];
	father[x] = Find(father[x]);  //找到根节点 ,
	transport[x] +=transport[tmp]; //根节点的第一层子节点+上根节点,,第二层+上第一层。。。第n层+上n-1层;
	return father[x];
}
并且原来的子节点全部接到了根节点上,再次查询并不会再次累加tranport

【源代码】

#include<cstdio>
using namespace std;
const int maxn =10010;
int father[maxn];
int num[maxn];
int transport[maxn];
void MakeSet(){
	for(int i=0;i<maxn;i++){
		father[i]=i;
		num[i]=1;
		transport[i]=0;
	}
}
int Find(int x){
	if(x==father[x])
		return x;
	int tmp = father[x];
	father[x] = Find(father[x]);
	transport[x] +=transport[tmp]; //本身的移动次数,加上父亲的;
	return father[x];
}
void Union(int x, int y){
	int xr = Find(x);
	int yr = Find(y);
	if(xr==yr) return ;
	father[xr] = yr;
	transport[xr] = 1; //移动过一次,就初始化为1;
	num[yr]+=num[xr];
	num[xr]=0;
}
int main(){
	int t;
	scanf("%d",&t);
	int cas=0;
	while(t--){
		int n,q;
		scanf("%d%d",&n,&q);
		char ch;
		int v1,v2,city;
		int sign=0;
		
		MakeSet();
		for(int i=0;i<q;i++){
			scanf(" %c",&ch);
			if(ch=='T'){
				scanf("%d%d",&v1,&v2);
				Union(v1,v2);
			}
			else if(ch=='Q'){
				scanf("%d",&city);
				if(!sign){
					printf("Case %d:\n",++cas);sign=1;
				}
				int x = Find(city);
				printf("%d %d %d\n",x,num[x],transport[city]);
				
			}
		}
	}
	return 0;
}


你可能感兴趣的:(图论)