Dragon BallsTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4165 Accepted Submission (s): 1603
Problem Descriptionfive Hundred years later, the number of Dragon Balls would increase unexpectedly, so it ' s too difficult For Monkey King (WuKong) to gather all of the dragon balls together.
His country have n cities and there is exactly N dragon balls in the world. At first, for the ith Dragon Ball, the sacred Dragon would puts it in the ith city. Through long years, some cities ' Dragon Ball (s) would is 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 would collect the information of one Dragon Ball, he'll ask you the information of the. You must tell him which city the ball was located and how many dragon balls was there in so city, you also need to tell H Im how many times the ball had been transported so far.
Inputthe first line of the input was a single positive integer T (0 < T <= 100).
For each case, the first line contains the 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 is in the same city with A has been transported to the city of the Bth ball in. You can assume that the cities is different.
Q A:wukong want to know X (the ID of the city Ath Ball are in), Y (the count of balls in Xth city) and Z (the tranporting Times of the Ath ball). (1 <= A, B <= N)
Outputfor each test case, output the test Case number formated as sample output. Then for each query, the output a line with three integers X Y Z saparated by a blank space.
Sample Input
3T 1 2T 3 2Q 4T 1 2Q 1T 1 3Q 1
Sample Output
Case 1:2 3 0Case 2:2 2 13 3 2
Test instructions: Test instructions: At the beginning each city has a corresponding number of balls, now gives 2 commands, one is t a B will be in the cities where all the ball transfer to the city of B Ball, a c A is required to output a ball in the city's number and how many balls in the city, as well as the number of a ball is transferred. Idea: First here just start the number of the ball and the city's number one by one corresponds to think about and check the set of methods, and then the operation of the T command is to merge A and B city, as long as the statistics of the city ball number of the array update can be, and a city's root node is located in the ball transfer number set to 1, Because obviously this root node corresponds to the ball is the first time to be shifted, the difficulty is that the other ball transfer times need to be based on the number of transfers of the root node to find out. The key is the path compression, in order to find a ball of the transfer times, it needs to update the path, has been updated to its root node, according to the root node of the transfer number of 1, backtracking to roll out the required number of ball transfer. Weighted and checked, the recursive process is very difficult to understand. 2015,7,23
#include <stdio.h> #include <string.h> #define M 10100int f[m],time[m],city[m];//respectively, parent node, number of transfers, number of beads inside the city void Init () {for (int i=0;i<m;i++) {f[i]=i;//parent node initialized to its own city[i]=1;//at the beginning of each city has a ball time[i]=0;}} int find (int k) {if (f[k]!=k) {int temp=find (f[k]);//Find the root node time[k]+=time[f[k]];//the number of transfers of the child nodes plus the number of transfers of the parent node f[k]=temp;//the compression path, The parent node points directly to the root node}return f[k]; } void Merge (int a,int b) {int x=find (a); int y=find (b); if (x!=y) {f[x]=y;time[x]=1;//first transfer city[y]+=city[x];city[x]=0;}} int main () {int T,v=1,i,a,b,c,n,m;char ch[5];scanf ("%d", &t), while (t--) {init ();p rintf ("Case%d:\n", v++); scanf ("% d%d ", &n,&m); for (i=0;i<m;i++) {scanf ("%s ", ch), if (ch[0]== ' Q ') {int k; scanf ("%d ", &c); K=find (c); printf ("%d%d%d\n", k,city[k],time[c]); } if (ch[0]== ' T ') {scanf ("%d%d", &a,&b), merge (A, b);}}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 3635 Dragon Balls