(Hdu step 5.1.5) Dragon Balls (the number of root nodes, nodes, and junctions of a set)

Source: Internet
Author: User


Topic:

Dragon Balls
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 562 Accepted Submission (s): 239
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 t He dragon Balls together. 

His country have n cities and there are exactly n Dragon Balls I n 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 in Teger T (0 < T <=).  
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 th E 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)  
Output
For each test case, the output of 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 Input
3T 1 2T 3 2Q 4T 1 2Q 1T 1 3Q 1
Sample Output
Case 1:2 3 0Case 2:2 2 13 3 2
Authorpossessor WC
Source2010 acm-icpc multi-university Training Contest (+)--host by HDU
Recommendlcy



Main topic:

At first, the ball I was placed in the city of I, in the case of the change in time, the circumstances of the changes, the ball was shifted, and the transfer, and the city's all the ball has been moved: T a B (a ball in the city where all the balls are moved to the B-ball city), q A (Q: A ball in How many balls are there in the city of a ball? How many times has a ball been shifted? )。


Topic Analysis:

and check the set. The root node, the number of nodes, and the number of moves per node of the set. Note that the number of moves per node is required. If the

When merging a brute force to find the number of moves, it will be tle, so here is the number of moves per node in the path compression.

T A b indicates that all dragon beads in the city where the dragon beads are indexed to A are moved to the city where the Dragon Beads index B is located

Q A. Query the following data: A dragon bead in the city's index, a dragon beads in the city's number of dragon beads, a dragon bead number of moves


The code is as follows:

/* * e.cpp * * Created on:2015 March 2 * author:administrator * * #include <iostream> #include <cstdio>using  namespace Std;const int maxn = 10001;int father[maxn];int nums[maxn];int move[maxn];int n;void init () {int i;for (i = 1; i < MAXN; ++i) {Father[i] = i;nums[i] = 1;move[i] = 0;}} int find (int a) {if (a = = Father[a]) {return A;} Use path compression to find the number of times the node is moved int temp = Father[a];father[a] = find (Father[a]);//do not swap with the following line of code. Will WA's move[a] + + move[temp];// Number of moves per node = own number of moves + number of moves of the Father node return father[a];} void Join (int a,int b) {int fa = find (a); int fb = find (b); if (FA! = FB) {FATHER[FA] = fb;nums[fb] + = nums[fa];//The key code for the number of nodes in the set MOVE[FA] = 1;}} int main () {int t;scanf ("%d", &t), int k;for (k = 1; k <= T; ++k) {printf ("Case%d:\n", k); init ();//int n;int q;scanf (" %d%d ", &n,&q);//string Str;char str[2];int i;for (i = 1; I <= Q; ++i) {//cin >> str;//input data size is large, do not use CIN, otherwise TLESCANF ("%s", str), if (str[0] = = ' T ') {int a,b;scanf ("%d%d", &a,&b), join (b);} else if (str[0] = = ' Q ') {int A;SCANF ("%d", &a);p rintf ("%d%d%d\n", find (a), Nums[find (a)],move[a]);}} return 0;}









(Hdu step 5.1.5) Dragon Balls (the number of root nodes, nodes, and junctions of a set)

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.