ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

并查集1.0

2021-01-08 18:32:29  阅读:207  来源: 互联网

标签:city ball 1.0 老大 int 查集 dragon find


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有