Step5.1.6 3635
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 456 AcceptedSubmission(s): 195
Problem Description
Five hundred years later, the number ofdragon balls will increase unexpectedly, so it\\\\\\\'s too difficult forMonkey King(WuKong) to gather all of the dragon balls together.
His country has N cities and there areexactly N dragon balls in the world. At first, for the ith dragon ball, thesacred dragon will puts it in the ith city. Through long years, somecities\\\\\\\' dragon ball(s) would be transported to other cities. To savephysical strength WuKong plans to take Flying Nimbus Cloud, a magical flyingcloud to gather dragon balls.
Every time WuKong will collect theinformation 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 arethere in that city, you also need to tell him how many times the ball has beentransported so far.
Input
The first line of the input is a singlepositive integer T(0 < T <= 100).
For each case, the first line contains twointegers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines containseither a fact or a question as the follow format:
T AB : All the dragon balls which are in the same city with A have beentransported to the city the Bth ball in. You can assume that the two cities aredifferent.
Q A: WuKong want to know X (the id of the city Ath ball is in), Y (the count ofballs 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 sampleoutput. Then for each query, output a line with three integers X Y Z saparatedby 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 TrainingContest(19)——Host by HDU
Recommend
lcy
题解:
并查集的题目,n个城市n个龙珠,龙珠可移动,移动时,要将整个城市里的龙珠移到另一个城市里。Q问的是有在a龙珠在哪个城市,该城市有多少个龙珠,a到该城市走了多久。其中a到达某城市及找a的父亲,而父亲城市有多少龙珠可是通过每次移动的龙珠累加得到,而a到达某城市的距离:a并不是每一次移动都有记录移动的距离,而是a的距离(包括a到父节点的一个距离)加上a当前父节点的距离(父节点到将转移城市的距离)。
源代码:
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
struct
{
intparent;
intrank;
intnum;
} dragon[10500];
int t,n,q;
void init()
{
for(inti = 1;i <= n;i++)
{
dragon[i].parent= i;
dragon[i].rank= 0;
dragon[i].num= 1;
}
}
int find_parent(int a)
{
if(dragon[a].parent== a)
returna;
intp = dragon[a].parent;
dragon[a].parent= find_parent(dragon[a].parent);
dragon[a].rank+= dragon[p].rank;
returndragon[a].parent;
}
void Union(int a,int b)
{
intx = find_parent(a);
inty = find_parent(b);
if(x== y)
return;
dragon[x].parent= y;
dragon[y].num+= dragon[x].num;
dragon[x].rank= 1;
}
int main()
{
cin>> t;
for(inti = 0;i < t;++i)
{
scanf("%d%d",&n,&q);
init();
inta,b;
printf("Case%d:\n",i+1);
for(intk = 0;k < q;k++)
{
charc[10];
scanf("%s",c);
if(c[0]== 'T')
{
scanf("%d%d",&a,&b);
Union(a,b);
}
else
{
scanf("%d",&a);
b= find_parent(a);
printf("%d%d %d\n",b,dragon[b].num,dragon[a].rank);
}
}
}
return 0;
}