hdu3635 Dragon Balls

Source: Internet
Author: User

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

This question examines and checks the path compression, this question lets me to the path compression to have the further understanding, the test instructions is initially, has the N Dragon bead, the number from 1 to N, respectively corresponds in the number from 1 to n the city. 2 Types of operation:

T a B, which means to transfer all the dragon beads from the city where a ball is located to City B. Q A, which indicates query a. The information requested is: a now in the city, a city of the number of Dragon beads, a transfer to the city of the number of moves (if not moved on the output 0) Here is the difficulty is to find out the number of times the ball is asked, here set a function Zhuanyi[n], initialized to 0, each move, The number of ancestors transferred to this ball is 1 (in fact, each city for the ancestors of the movement at most only once, the others are moving with their ancestors), and then use and check the method of recursive compression path, so that the current point plus all of their ancestors transfer times, and then make their ancestors into the current transfer of the city, This will ensure that the next time you do not repeat Add. #include <stdio.h> #include <string.h>int pre[10006],zhuanyi[10006],num[10006];char s[10];int Find (int x) {int temp;if (X==pre[x]) return x;TEMP=PRE[X];Pre[x]=find (Pre[x]);ZHUANYI[X]+=ZHUANYI[TEMP];return pre[x];} int main () {int t,n,m,i,j,a,b,c,t1,t2,num1=0;scanf ("%d", &t);while (t--){num1++;printf ("Case%d:\n", NUM1);scanf ("%d%d", &n,&m);for (i=1;i<=n;i++) {Pre[i]=i;zhuanyi[i]=0;num[i]=1; Pre[] Indicates the ball's father, zhuangyi[] indicates the number of ball transfers, Num[i] The number of balls in the city where the ball I is located}for (i=1;i<=m;i++) {scanf ("%s", s);if (s[0]== ' T ') {scanf ("%d%d", &a,&b);T1=find (a); T2=find (b);if (T1==T2) continue;Pre[t1]=t2;NUM[T2]+=NUM[T1];Zhuanyi[t1]=1;}else if (s[0]== ' Q ') {scanf ("%d", &a);T1=find (a);printf ("%d%d%d\n", T1,num[t1],zhuanyi[a]);}}}return 0;}

hdu3635 Dragon Balls

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.