HDU 3635 Dragon bils (query set)

Source: Internet
Author: User

HDU 3635 Dragon bils (query set)

Dragon bils Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3360 Accepted Submission (s): 1303


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 1:2 3 0Case 2:2 2 13 3 2

Authorpossessor WC

The third result of this question made me think about it the next morning when I went to the hospital.

There are n longzhu and n cities in total, and the I-th longzhu is in the I-th city. The following two operations are available:
T a B: Move all the dragon beads in the city where A dragon beads are located to the city where B is located.
Q a: How many longzhu and longzhu are moved in the city where A is located?

Idea: the first two questions are well solved. The third question is the number of times longzhu was moved. If the compression path is not applicable to this number of times, the number of times is the path length from the node to the root node (which I want to understand in the morning). Now the compression path is used, we need to save the path length while compressing it.

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; int pre [10010]; int trans [10010]; int ranks [10010]; int union_find (int node) {int temp; if (node = pre [node]) return node; else {temp = pre [node]; pre [node] = union_find (pre [node]); trans [node] + = trans [temp];} return pre [node];} int main (int argc, char * argv []) {// freopen ("3635.in ", "r", stdin); int T, N, Q, a, B; scanf ("% d", & T); char s [10]; int total = T; while (T --) {scanf ("% d", & N, & Q); print F ("Case % d: \ n", total-T); memset (pre, 0, sizeof (pre); memset (trans, 0, sizeof (trans )); memset (ranks, 0, sizeof (ranks); for (int I = 1; I <= N; ++ I) {pre [I] = I; ranks [I] = 1;} while (Q --) {scanf ("% s", s); if (s [0] = 'T ') {scanf ("% d", & a, & B); int p = union_find (a); int q = union_find (B); if (p! = Q) {ranks [q] + = ranks [p]; // The number of nodes in B increases the number of nodes in a. pre [p] = q; // graft the root of a to the root of B. trans [p] ++; ranks [p] = 0 ;}} else {scanf ("% d", & ); union_find (a); printf ("% d \ n", pre [a], ranks [pre [a], trans [a]);} return 0 ;}
     
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.