Dragon bils
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3242 accepted submission (s): 1250
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 to 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 the city ath ball is in), y (the Count of Ballin 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, output a line with three integers x y z saparated by a blank space.
Sample Input
23 3T 1 2T 3 2Q 23 4T 1 2Q 1T 1 3Q 1
Sample output
Case 3 0 case 2 13 3 2 well, I admit this question is so powerful: t a B indicates that the set where A is located is hung on B, q a asks three information in total: ① The set where A is located (this is easy to handle, the return value of find (a) is the answer); ② the number of elements in the set where A is located, (this is also very easy. You can use a size [] array to maintain it during merging.) ③ the number of A moves makes it difficult, because path compression will damage the original tree structure. If I set a tTime [] array, we need to maintain it during path compression, such as T 1 2, T 1 3 when 1 and 3 are merged, it is actually Fa [2] = 3; tTime [2] ++; at this time, tTime [1] is still equal to 1, therefore, when we search for 1, the first ancestor of 1 is 2, and then tTime [1] + = tTime [2]; that is, we trace back to the ancestor, add the number of transfers to the children and grandchildren. Then we will find that the ancestor of 2 is 3. I think this is a little difficult to check the set, but I have encountered a more difficult one before, with the right and the query set. Each element in the SET has a relationship with each other (like a picture, value between two vertices.# Include <cstdio> # include <iostream> # include <cstring> # include <cctype> # include <cstdlib> # include <algorithm> # include <vector> # include <queue> # include <stack> # include <set> # include <map> # include <list> using namespace STD; const int maxn = 50100; const int INF = 1 <25; int Fa [maxn], size [maxn], tTime [maxn]; void make_set (int n) {for (INT I = 1; I <= N; I ++) {Fa [I] = I; Size [I] = 1; tTime [I] = 0 ;}} int find (INT X) {If (X! = Fa [x]) {int T = Fa [X]; Fa [x] = find (Fa [x]); tTime [x] + = tTime [T]; // maintain the tTime array return Fa [X];} return X;} void Union (int x, int y) {int FX = find (X ); int FY = find (y); If (FX = FY) return; Fa [FX] = FY; Size [FY] + = size [FX]; tTime [FX] ++;} int main () {int A, B, t, n, m, CAS = 1; char C [5]; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); printf ("case % d: \ n ", CAS ++); make_set (n); While (M --) {scanf (" % s ", C ); if (C [0] = 'T') {scanf ("% d", & A, & B); Union (a, B );} else {scanf ("% d", & A); int ans = find (a); printf ("% d \ n", ANS, size [ANS], tTime [a]) ;}} return 0 ;}
HDU 3635-Dragon bils (Advanced query set)