Dragon Balls
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4384 Accepted Submission (s): 1673
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 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 Test instructions: Enter two numbers when you encounter T X-y represents the transfer of the Dragon Ball to y city, Q input A for the City B of the dragon beads in the city B, the total number of dragon pearls in the cities, and the transfer of the first dragon beads. Number of key points: the most difficult to find the number of times the Dragon Bead Transfer, when the parent node of the Dragon Bead Transfer, the Dragon beads also follow the transfer
#include <stdio.h> #include <string.h> #define MAX 20000int set[max];int path[max];int time[max];int Find ( int fa) {int t; if (Fa==set[fa]) return FA; T=SET[FA]; Set[fa]=find (Set[fa]); TIME[FA]+=TIME[T]; return SET[FA];} void Mix (int x,int y) {int fx;int fy;fx=find (x); Fy=find (y); if (fx!=fy) { set[fx]=fy; PATH[FY]+=PATH[FX]; time[fx]++;}} int main () {int T,n,m,x,y,b,i;char a;scanf ("%d", &t), int k=0;while (t--) {scanf ("%d%d", &n,&m);p rintf ("case %d:\n ", ++k); for (i=1;i<=n;i++) {set[i]=i;path[i]=1;time[i]=0;} while (m--) {GetChar (); scanf ("%c%d ", &a,&x if (a== ' T ') {scanf ("%d", &y); Mix (x, y);} Else{y=find (x);//You must use a variable value to represent find (x) printf ("%d%d%d\n", y,path[y],time[x]);}} return 0;}
Hdoj 3635 Dragon Balls "and check the node transfer number + nodes + Point root node"