Dragon Balls
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 Input2 3 3 T 1 2 T 3 2 Q 4T 1 2Q 1T 1 3Q 1
Sample outputcase 1:2 3 0 Case 2:2 2 1 3 3 2
1#include <cstdio>2 using namespacestd;3 4 Const intmaxn=10005;5 6 intBIN[MAXN],RANK[MAXN],CNT[MAXN];7 8 intFindintx)9 { Ten if(X==bin[x])returnx; One intt=Bin[x]; ABIN[X] =find (bin[x]); -CNT[X] + =Cnt[t]; - returnBin[x]; the } - - - voidMergeintXinty) + { - intfx=find (x); + intfy=find (y); A if(fx!=fy) at { -bin[fx]=fy; -rank[fy]+=RANK[FX]; -cnt[fx]=1; - } - } in - intMain () to { + //freopen ("In.txt", "R", stdin); - CharC; the intt,kase,n,q,i,a,b; *scanf"%d",&t); $Kase=1;Panax Notoginseng while(t--) - { theprintf"Case %d:\n", kase++); +scanf"%d%d",&n,&q); A for(i=1; i<=n;i++) the { +bin[i]=i; -cnt[i]=0; $rank[i]=1; $ } - while(q--) - { the GetChar (); -scanf"%c%d",&c,&a);Wuyi if(c=='T') the { -scanf"%d",&b); Wu merge (A, b); - } About Else $ { - inttemp=find (a); -printf"%d%d%d\n", Bin[temp],rank[temp],cnt[a]); - } A } + } the return 0; -}
HDU 3635 Dragon Balls