Dragon bils
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1271 accepted submission (s): 516
Problem descriptionfive
Hundred years later, the number of Dragon bils will increase
Unexpectedly, so it's too difficult for Monkey King (Wukong) to gather
All of the dragon bils together.
His
Country has n cities and there are exactly n dragon bils 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) wocould be
Transported to other cities. To save physical strength Wukong plans
Take flying nimbus cloud, a magical flying cloud to gather dragon bils.
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 bils are there in that
City, you also need to tell him how many times the ball has been
Transported so far.
Inputthe first line of the input is a single positive integer T (0 <= 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 bils 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
City ath ball is in), y (the Count of Ballin Xth City) and Z (
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, output a line with three integers x y z saparated
By a blank space.
Sample input23 3 T 1 2 T 3 2q 23 4 T 1 2q 1 T 1 3q 1
Sample outputcase 3 0 case 2 13 3 2 analysis: (1) this question is a query set. There is nothing special. The key is to record the transfer count. Trans_times [A] + = Trans_times [TEM]; // Update the number of transmissions ,. To understand this sentenceCode (2) Update the total number of balls in the city where the ball is located. City [y] + = City [x]; Understanding this line of code (3) others are simple and set query routines. After understanding these two lines, this question is over...
# Include <stdio. h> # Define Max_num10010Int N, Q; // Record the number of dragons in the city Int City [max_num]; // Set to which the ball points Int Root [max_num]; Int Trans_times [max_num]; Int Find_root ( Int A ){ If (Root [a] =)Return A; Else { Int TEM = Root [a]; root [A] = Find_root (root [a]); trans_times [A] + = Trans_times [TEM]; // Update transfer count // The ball of the city was transferred to the root, // The number of balls in this city is reduced by 1, while the number of balls in the root city is increased by 1. Return Root [a] ;}} Void Union_set ( Int A, Int B ){ Int X = Find_root (); Int Y = Find_root (B ); If (X = y) Return ; Else {City [y] + =City [X]; root [x] = Y; trans_times [x] ++ ;}} Void Init (){ For ( Int I = 0 ; I <= N; I ++ ) {Root [I] = I; City [I] = 1 ; Trans_times [I] = 0 ;}} Int Main (){ Int T; Int X, Y, FX, fy, Z; Char OP; scanf ( " % D " ,& T ); For ( Int T = 1 ; T <= T; t ++) {Scanf ( " % D " , & N ,& Q); Init (); printf ( " Case % d: \ n " , T ); While (Q -- ) {Getchar (); scanf ( " % C " ,& OP ); // Printf ("% C", OP ); If (OP = ' T ' ) {Scanf ( " % D " , & X ,& Y); FX = Find_root (x); FY = Find_root (y ); If (FX! =FY) union_set (x, y );} Else {Scanf ( " % D " ,& Z); find_root (z); printf ( " % D \ n " , Root [Z], City [root [Z], trans_times [Z]) ;}} Return 0 ;}