其他分享
首页 > 其他分享> > 并查集1.0

并查集1.0

作者:互联网

Dragon Balls

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个城市各有一个人,每次给你 T a和b 表示a带着小弟跟着b混,Q a的就是问你为你a现在再那个城市,他在的城市有多少人,他换了几次城市。

#include<stdio.h>
int a[10086],b[10086],c[10086];//老大,城市球数量,转移数 
int all;//要输出的转移总数 
void red(int n){//还原函数 
	int i=1;
	for(i;i<=n;i++){
		a[i]=i;//让它认自己为老大先 
		b[i]=1;//每个城市初始的球的数量为1 
		c[i]=0;
	} 
}
int find(int i){//查询老大函数 
	if(a[i]==i){//如果他老大是他自己,那他就是顶头老大 
		return i;
	}
	else find(a[i]);//老大不是他自己,就再找老大 
}
int find_2(int i){//这个寻找老大函数二号,是为了解决步数问题,要看一
	all=all+c[i];//个人的总步数,只要把自己的步数再加上老大们的步数 。 
	if(a[i]==i){
		return i;
	}
	else{
		find_2(a[i]);
	}	
}
int main()
{
	int k,l=1;//要测试的次数 
	int n,m;//城市数和命令次数 
	int d,e;//要移动的球或要查询的球,和要目标球 
	int j,h;
	char f;//命令种类 
	scanf("%d",&k);
	while(k--){
		printf("Case %d:\n",l);
		l++;
		scanf("%d%d",&n,&m);
		red(n); 
		while(m--){
			getchar();//防止换行影响程序 
			scanf("%c",&f);
			if(f=='T'){//判断命令 
				scanf("%d%d",&d,&e);
				j=find(d);h=find(e);//找到d和e的顶头老大 
				b[h]=b[h]+b[j];//把d所在城市的球移给e 
				b[j]=0;
				c[j]++;//让老大的移动次数加1,小弟们不用,输出时还要再加上 
				a[j]=h;//d的顶头老大认 e的顶头老大为老大 
			}
			else{
				all=0;
				scanf("%d",&d);
				j=find_2(d);//找老大的同时顺便把步数计算 
				printf("%d %d %d\n",j,b[j],all);
			}
		}
		
	}
	return 0;
}

标签:city,ball,1.0,老大,int,查集,dragon,find
来源: https://blog.csdn.net/m0_54351445/article/details/112377075