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;
}

 

全部评论

相关推荐

头像
10-22 20:13
中南大学 Java
序言大家好呀。我是希晨er,一个初入职场的程序猿小登最近上班摸鱼刷到了一篇文章:10年深漂,放弃高薪,回长沙一年有感,还有聊聊30岁大龄程序员过往的心路历程,突然就有点感慨。我如今也做出了和大明哥一样的抉择,只是更早。此外我22年的人生,好像从来没好好记录过。正好现在工作不太忙,就想把这些经历写下来,也希望能得到社区里各位前辈的指点个人背景我是03年出生的西安娃,父母都是普通打工人。刚从中南大学软件工程专业毕业半年,现在在老家的央企过着躺平摆烂的日子成长轨迹从农村到城市的童年我家并不是西安的,只是爸妈在西安上班,从小学之后就把我接到了西安。后来老家房子拆了,爷爷奶奶也搬了过来。农村的生活,我觉...
Yki_:看哭了,恋爱那一段你女朋友说你不够关心她,可你毕竟也愿意遇到矛盾写几千字来和她慢慢分析;说不愿意给她花钱,我感觉可能只是消费观不一样;如果她想留在长沙,也应该提前跟你说开。不过她也许会心疼你放弃大厂offer转向数字马力?我也因为同样的原因有过一段幸福而充满遗憾的感情,不过跟爱情相比确实前途更重要一点。至于offer的选择,换我我也会这么选。把这些旧事记录下来以后,接下来就好好向前看吧,加油兄弟
🍊晨光随笔
点赞 评论 收藏
分享
東大沒有派對:这是好事啊(峰哥脸
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务