【带权并查集】Dragon Balls
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1201 Accepted Submission(s): 484
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.
![](http://acm.hdu.edu.cn/data/images/C308_1004_1.jpg)
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.
![](http://acm.hdu.edu.cn/data/images/C308_1004_1.jpg)
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)
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
Author
possessor WC
Source
Recommend
lcy
题目大意:
输入T,有T组测试数据。
每一组测试输入先输入N,M,表示有N个城市,N个龙珠,有M个询问。
接下来有Q行,每一行先输入一个字符及数字:
T a b:表示龙珠a所在的城市的所有龙珠都迁移到b龙珠所在的城市。
Q a:表示询问a龙珠的信息,分别输出a龙珠所在的城市编号,a龙珠所在城市的龙珠数量,a龙珠迁移了几次、
题解:
难点在于如何求迁移的次数,用并查集的方法来统计所在城市的龙珠数量也比较简单。(Count[B]+=Count[A])
而对于迁移次数的统计,则用一个数组来统计,Time[i],表示i龙珠的迁移次数,初始值均为0.
当执行T a b的时候,A=Find(a);只需要把Time[A]=1,然后再查找子节点的时候,回溯累加即可、
代码:2015.7.27
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #define MAX 10086 4 using namespace std; 5 int ID[MAX]; 6 int Count[MAX]; 7 int Time[MAX]; 8 void Cread(int N) 9 { 10 int i; 11 for(i=1;i<=N;i++) 12 {ID[i]=i;Count[i]=1;Time[i]=0;} 13 } 14 15 int Find(int x) 16 { 17 int TMD; 18 if(x==ID[x])return x; 19 else 20 { 21 TMD=ID[x]; 22 ID[x]=Find(ID[x]); 23 Time[x]+=Time[TMD]; 24 } 25 return ID[x]; 26 } 27 28 void Update(int a,int b) 29 { 30 int A,B; 31 A=Find(a);B=Find(b); 32 if(A!=B) 33 { 34 ID[A]=B; 35 Time[A]=1; 36 Count[B]+=Count[A]; 37 Count[A]=0; 38 } 39 } 40 int main() 41 { 42 int T,N,Q,i,j,a,b,t=1; 43 char str[5]; 44 scanf("%d",&T); 45 while(T--) 46 { 47 printf("Case %d:\n",t++); 48 scanf("%d%d",&N,&Q); 49 Cread(N); 50 while(Q--) 51 { 52 scanf(" %s",str); 53 if(str[0]=='T') 54 { 55 scanf(" %d %d",&a,&b); 56 Update(a,b); 57 } 58 else 59 { 60 scanf(" %d",&a); 61 b=Find(a); 62 printf("%d %d %d\n",b,Count[b],Time[a]); 63 } 64 } 65 } 66 return 0; 67 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************