Hangzhou Electric 3635-dragon Balls (and check set)

Source: Internet
Author: User

Dragon BallsTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)Total Submission (s): 4582 Accepted Submission (s): 1747

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 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

Test instructions: There are n balls, marking 1,2.3 ... Originally, respectively, in the respective marking of the city of ... Later after a few transforms now let you ball some Ball State:

Two operations:

T a B means to add all the balls in the city where a ball is located to the city where B-ball is located

Q A indicates the city in which the output a is located , the number of balls in the city wherea is located,the number of times a is transferred

Analysis:

Can think of is to use and check set, the first two problems are very good solution, one to find the root node another find the total number of elements in this tree, difficult is the third problem, how to ask for the number of transfers?

We can set an array to record, each time to update, obviously if in the merge function to calculate the formula is very troublesome, we use another method, in the merge function, we only let the transfer of the root node plus one, the other, before the next merge we want to compress the path, compression process, we have to update the number of transfers, But remember that we must update from the top down, this process can only be achieved by recursion plus backtracking!



#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int city[10100],per[ 10100],num[10100];int M,n,mov[10100];char s[3];void Init () {for (int i=1;i<=m;i++) {per[i]=i;num[i]=1;mov[i]=0;// Initialize array}}int find (int x) {//int t=x;//while (t!=per[t])//t=per[t];//int i=x,j;//while (i!=t)//{//j=per[i];//per[i]=t;// I=j;//}//return t;//begins with the following commented out section of the normal compression path if (X!=per[x]) {int t=per[x];p er[x]=find (t); mov[x]+=mov[t];//Update, The number of times a child node moves is equal to the number of times he moves and the number of times the parent node moves}return per[x];} void join (int x,int y)//merges two different numbers {int fx=find (x); int fy=find (y); if (fx!=fy) {//for (int i=1;i<=m;i++)//{//if (Find (i) = =FX)//mov[i]++;//}//This is what I started thinking, directly updated in the Join function, but decisively timed out per[fx]=fy;num[fy]+=num[fx];mov[fx]=1;//each time with the reception you can only move once, So only the root node is equal to 1 can be}}int main () {int n,a,b,cot=1;scanf ("%d", &n), while (n--) {scanf ("%d%d", &m,&n), init ();p rintf ("Case%d:\n", cot++);//control format for (int i=0;i<n;i++) {scanf ("%s", s);//The advantage of using%s input is that you do not have to process the absorption carriage return if (s[0]== ' T ')//two different operations { scanf ("%d%d", &a,&b); join (b);} ELSE{SCANF ("%d", &a);int T=find (a);//Find the root node of a, printf ("%d%d%d\n", T,num[t],mov[a]);}} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric 3635-dragon Balls (and check 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.