Dragon Balls
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4346 Accepted Submission (s): 1658
Five hundred years later, the number of Dragon Balls would increase unexpectedly, so it's too difficult for Monkey King (wuk ONG) 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 Input23 3T 1 2T 3 2Q 4T 1 2Q 1T 1 3Q 1
Sample outputcase 1:2 3 0Case 2:2 2 13 3 2 The main idea is the beginning of a city a Dragon Ball, if the input t a B is a ball in the city of the ball all transferred to the city where the B ball is input q A is to output a ball where the city number, a ball where The number of balls in the city, the number of times a ball moves
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMax 100025 using namespacestd;6 intFather[max],ran[max],num[max];7 intb;8 9 voidInit ()Ten { One inti; A for(i=1; i<=a;i++) - { -Father[i]=i;//Record parent Node theran[i]=1;//record the number of dragon beads in your city -num[i]=0;//record number of moves - } - } + - intFindintx) + { A if(X==father[x])returnx; at intt=Father[x]; -Father[x]=find (Father[x]);//compressed paths, all pointing to the root node -num[x]+=num[t];//the number of times each ball is moved equals the number of times it was moved plus the number of parent nodes - returnFather[x]; - } - in voidJoinintXinty) - { to intfx=find (x); + intfy=find (y); - if(fx!=fy) the { *Father[fx]=fy;//move one root node to another root node $RAN[FY]+=RAN[FX];//FY is the total number of root nodes equal to the number of two root nodes owned by the sumPanax Notoginsengnum[fx]=1;//the moved root node is moved for the first time - } the } + A intMain () the { + inti,n,m,n,cot=1; - CharC; $scanf"%d",&N); $ while(n--) - { -scanf"%d%d",&a,&b); the GetChar (); - init ();Wuyiprintf"Case %d:\n", cot++); the for(i=0; i<b;i++) - { Wuscanf"%c",&c); - if(c=='T') About { $scanf"%d%d",&m,&n); - GetChar (); - join (m,n); - } A Else + { thescanf"%d",&m); - GetChar (); $ intt=find (m);//To output all the number of root nodes theprintf"%d%d%d\n", T,ran[t],num[m]); the } the } the } - return 0; in}
Dragon balls--hdu3635 (and collection)