*HDU3635 并查集

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5972    Accepted Submission(s): 2194


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
 
Author
possessor WC
 
Source
 
题意:
刚开始每个龙珠在对应的城市,T 1,2 表示1龙珠所在的城市里的所有龙珠转移到2龙珠所在的城市中。Q 1 表示询问1龙珠在哪个城市,该城市有几个龙珠,该龙珠的转移次数。
代码:
 1 //重点是转移次数,和转移量,转移量就是一个根合并到另一个根时,把数量加到另一个根上,每个点的转移次数是这个点的加上他的
 2 //父亲的。一个点合并到一个集合里后注意次数就是这个点合并到该集合之前的转移次数加上该集合根节点的转移次数。
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<cstring>
 6 using namespace std;
 7 int t,n,m;
 8 int fat[10004];
 9 int cnt[10004],num[10004];
10 int find(int x)
11 {
12     if(fat[x]!=x)
13     {
14         int tem=fat[x];  //保存下他的父亲。后面路径压缩直接指向根了。
15         fat[x]=find(fat[x]);
16         num[x]+=num[tem];
17     }
18     return fat[x];
19 }
20 void connect(int x,int y)
21 {
22     int a=find(x),b=find(y);
23     if(a!=b)
24     {
25         fat[a]=b;
26         cnt[b]+=cnt[a];
27         num[a]++;      // 初始转移了一次。
28     }
29 }
30 int main()
31 {
32     int a,b;
33     char ch[2];
34     scanf("%d",&t);
35     for(int k=1;k<=t;k++)
36     {
37         printf("Case %d:
",k);
38         scanf("%d%d",&n,&m);
39         for(int i=0;i<=n;i++)
40         {
41             fat[i]=i;
42             cnt[i]=1;
43             num[i]=0;
44         }
45         for(int i=1;i<=m;i++)
46         {
47             scanf("%s",ch);
48             if(ch[0]=='T')
49             {
50                 scanf("%d%d",&a,&b);
51                 connect(a,b);
52             }
53             else
54             {
55                 scanf("%d",&a);
56                 int ans=find(a);
57                 printf("%d %d %d
",ans,cnt[ans],num[a]);
58             }
59         }
60     }
61     return 0;
62 }
 
原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6017376.html