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