Dragon Balls
http://acm.hdu.edu.cn/showproblem.php?pid=3635
题意:给出n个龙珠,开始的时候第i个龙珠在第i个城市,然后下面有q个操作
T a b:把第a个龙珠所在城市的所有龙珠移动到第b个龙珠所在的城市
Q a :询问第a个龙珠所在的城市,这个城市有多少颗龙珠,这颗龙珠被移动多少次
思路:第a个龙珠所在的城市与这个城市有多少颗龙珠比较简单用并查集找到结点a的根结点与根结点的秩就行了,而龙珠a被移动的次数比较麻烦,我们讲合并两个集合的过程看做第一个集合的根结点移动次数+1,那对于任意一个结点就可以用cnt[a]+cnt[find(a)]表示a结点移动的次数,在合并的时候由于根结点改变,所以要更新cnt的值。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8
26 Accepted Submission(s): 373
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define Max 10001 5 int p[Max],flag[Max],rank[Max]; 6 int n; 7 void init() 8 { 9 for (int i=1;i<=n;i++) 10 { 11 p[i]=i; 12 flag[i]=0; 13 rank[i]=1; 14 } 15 } 16 17 int find(int x) 18 { 19 if (p[x]!=x) 20 { 21 int k=p[x]; 22 p[x]=find(p[x]); 23 flag[x]+=flag[k]; 24 } 25 return p[x]; 26 } 27 28 void uion(int x,int y) 29 { 30 x=find(x); 31 y=find(y); 32 p[x]=y; 33 flag[x]++; 34 rank[y]+=rank[x]; 35 } 36 37 int main() 38 { 39 int T,m,i,a,b; 40 scanf("%d",&T); 41 char q; 42 for (int j=1;j<=T;j++) 43 { 44 printf("Case %d:\n",j); 45 scanf("%d%d",&n,&m); 46 init(); 47 for (i=0;i<m;i++) 48 { 49 getchar(); 50 scanf("%c",&q); 51 if (q=='T') 52 { 53 scanf("%d%d",&a,&b); 54 uion(a,b); 55 } 56 else 57 { 58 scanf("%d",&a); 59 int k=find(a); 60 printf("%d %d %d\n",k,rank[k],flag[a]+flag[k]); 61 } 62 } 63 } 64 return 0; 65 } 66 /* 67 68 #include<iostream> 69 #include<cstdio> 70 #include<cstring> 71 #include<algorithm> 72 using namespace std; 73 #define N 10010 74 75 struct node 76 { 77 int parent; //¸ù½Úµã 78 int son; //×Ó½Úµã 79 int transport; //תÒÆ´ÎÊý 80 }p[N]; 81 82 int find(int x) 83 { 84 int temp; 85 if(x == p[x].parent) 86 return x; 87 else 88 { 89 temp = p[x].parent; 90 p[x].parent = find(p[x].parent); 91 p[x].transport += p[temp].transport; //ÕâµãÐèÒª·´¸´´§Ä¦ 92 } 93 return p[x].parent; 94 } 95 96 void join(int x, int y) 97 { 98 int root1, root2; 99 root1 = find(x); 100 root2 = find(y); 101 if(root1 == root2) 102 return ; 103 p[root1].parent = root2; 104 p[root1].transport++; //¸ù½ÚµãתÒÆ´ÎÊý+1£¬ÒªÀí½â 105 p[root2].son += p[root1].son; 106 p[root1].son = 0; 107 } 108 109 int main() 110 { 111 int ncase, T = 1; 112 int num, querynum; 113 char ope; 114 int from, to; 115 int querycity; 116 int ans; 117 scanf("%d", &ncase); 118 while(ncase--) 119 { 120 scanf("%d%d", &num, &querynum); 121 for(int i = 1; i <= num; ++i) //³õʼ»¯ 122 { 123 p[i].parent = i; 124 p[i].son = 1; 125 p[i].transport = 0; 126 } 127 printf("Case %d:\n", T++); 128 for(int i = 0; i < querynum; ++i) 129 { 130 scanf("%*c%c", &ope); 131 if(ope == 'T') 132 { 133 scanf("%d%d", &from, &to); 134 join(from, to); 135 } 136 else 137 { 138 scanf("%d", &querycity); 139 ans = find(querycity); 140 printf("%d %d %d\n", ans, p[ans].son, p[querycity].transport); //¾À½áÁË3¸öСʱ~~ 141 } 142 } 143 } 144 return 0; 145 } 146 */