HDOJ-3635 Dragon Balls (并查集的查找)

来源:互联网 发布:淘宝如何优化关键词 编辑:程序博客网 时间:2024/05/22 04:01

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 878 Accepted Submission(s): 347 
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
23 3T 1 2T 3 2Q 23 4T 1 2Q 1T 1 3Q 1
 
Sample Output
Case 1:2 3 0Case 2:2 2 13 3 2
 
Author
possessor WC
 
Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU
 
Recommend
lcy

题意:每个序号代表的城市,有着和城市相同序号的球,球可以运送。

根据所给的信息:将a序号球所在城市的所有球,运送给b序号所在的城市。

求:某序号球所在的城市,该城市所有球的个数,该序号球运送过的次数。


刚开始把题意理解运送到b城市。WA了几次。而且也不知道输出的

Case 1:

应该放在哪里。涨知识了。

#include<iostream>  #include<cstdio>  #include<cstring>  #include<algorithm>  #include<cmath>  using namespace std;#define N 10010int father[N];//城市int cnt[N];//球的个数int time=0;//转移次数void init(int n){for (int i = 0; i < n; i++){father[i] = i;cnt[i] = 1;}}int find(int x)//不能压缩路径 {if (father[x] == x)return x; time++;return find(father[x]);}int main(){int t;scanf("%d", &t);int cas=1;while (t--){printf("Case %d:\n", cas++);int n, q;scanf("%d%d", &n, &q);init(n);for (int i = 0; i < q; i++){char c[10];int a, b;scanf("%s",c);if (c[0] == 'T'){scanf("%d %d", &a, &b);a--;//数据从1开始 数组从0开始 所以--b--;a = find(a);//a类型球所在的城市b = find(b);//b类型球所在的城市father[a] = b;cnt[b] += cnt[a];cnt[a] = 0;}if (c[0] == 'Q'){scanf("%d", &a);a--;time = 0;b = find(a);printf("%d %d %d\n", b+1, cnt[b], time);//因为前面b--了所以这里b+1}}}return 0;}



原创粉丝点击