Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4934 Accepted Submission(s): 1859
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
题目大意:
孙悟空找龙珠,每次他会询问一个龙珠,你要告诉他这个龙珠(是几龙珠,比如说四星球^0^)现在在的城市,这个城市共有几颗龙珠。然后龙珠刚开始都是分别在自己编号所对应的城市。然后经过时间的流逝,会被搬运,然后每次搬运都是要把这个城市所有的龙珠全部搬运。
个人对于output第二组数据的理解(感觉题目说的有点不是很清楚啊):
就是首先把一星球移动到第二个城市(也就是第一个T 1 2),然后询问Q 1(到这里应该都没有问题)。然后后来又来了一个T 1 2,个人感觉这个时候城市1不是应该没有龙珠了吗,但是答案却是一星球在第三个城市,刚开始也百思不得其解。。。
后来想了一下,T 1 2 和T 1 3指的是,T 1 2位刚开始把一星球所在的城市的所有的龙珠都转移到第二个城市,所以这个时候第二个城市里面有一星球和二星球。然后接着输入了T 1 3。这个时候指的是一星球所在的城市的所有球移动到第三个城市,所以这个时候第三个城市就有三个球了。输出才为3 3 2
综上的理解,我之所以刚开始没理解,是因为T a b的第一个数字a理解错了,a指的是a星球所在的城市的所有球
给出代码:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; int par[10000 + 50], level[10000 + 50]; int q, t, a, b, n; int sum[10000 + 50]; char s[5]; void init(){ for(int i = 1; i <= n; i++){ par[i] = i; level[i] = 1; sum[i] = 0; } } int find(int x){ if(par[x] == x)return x; int t = par[x]; par[x] = find(par[x]); sum[x]+=sum[t];//如果父亲节点移动了,那么子节点也在这个城市中。因为是全部移动的,所以也是移动了。 return par[x]; } void unite(int x, int y){ x = find(x); y = find(y); //printf("x = %d y = %d\n", x, y); if(x == y)return; par[x] = y; level[y] = level[y] + level[x]; sum[x] = 1; } int main(){ scanf("%d", &t); getchar(); int count1 = 0; while(t--){ count1++; scanf("%d%d", &n, &q); init(); getchar(); printf("Case %d:\n", count1); for(int i = 1; i <= q; i++){ scanf("%c", &s[0]); getchar(); if(s[0] == 'T'){ scanf("%d%d", &a, &b); getchar(); unite(a, b); } else if(s[0] == 'Q'){ scanf("%d", &a); getchar(); int f = find(a); printf("%d %d %d\n", f, level[f], sum[a]); } } } return 0; } </cstring></algorithm></cstdio>