Test the F-Dragon bils (and check the Set)

Source: Internet
Author: User
F-Dragon bils Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit status

Description

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

Input

The first line of the input is a single positive integer T (0 <t <= 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)

Output

For 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 2 13 3 2 There are n dragon beads. In 1 to n cities, all the dragon beads in the city where t a B mobile A is located to the city where B is located, q a outputs the city where A is located, the number of Dragon beads in the city, the number of times a moves, and queries the Set Problem. An Array records the number of moving dragon beads and the number of city dragon beads, t a B represents the number of times a moves + 1, and the number of longzhu moves to the city where A is located + 1, the number of longzhu in the city where B is located plus the number of cities in which a is located
#include <cstdio>#include <cstring>int p[11000] , c[11000] , num[11000] ;/*int f(int x){    int r , k , l , temp = 0 ;    r = x ;    while( r != p[r] )    {        r = p[r] ;        temp++ ;    }    k = x ;    while( k != r )    {        l = p[k] ;        c[k] += ( --temp ) ;        p[k] = r ;        k = l ;    }    return r ;}*/int f(int x){    if(x==p[x])        return x;    int t=p[x];    p[x] = f(p[x]);    c[x] += c[t];    return p[x];}void add(int u,int v){    u = f(u) ;    v = f(v) ;    if(u != v)    {        p[u] = v ;        num[v] += num[u] ;        num[u] = 0 ;        c[u]++ ;    }}int main(){    int t , tt , i , n , m , a , b ;    char ch ;    scanf("%d", &t);    for(tt = 1 ; tt <= t ; tt++)    {        printf("Case %d:\n", tt);        scanf("%d %d", &n, &m);        for(i = 1 ; i <= n ; i++)        {            p[i] = i ;            num[i] = 1 ;        }        memset(c,0,sizeof(c));        while(m--)        {            scanf("%*c%c", &ch);            if( ch == 'T' )            {                scanf("%d %d", &a, &b);                add(a,b);            }            else            {                scanf("%d", &a);                int k = f(a) ;                printf("%d %d %d\n", k, num[k] , c[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.