HDU 3635 dragon bils (query set)

Source: Internet
Author: User
Dragon bils

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3245 accepted submission (s): 1252


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 input23 3 T 1 2 T 3 2q 23 4 T 1 2q 1 T 1 3q 1

 

Sample outputcase 3 0 case 2 13 3 2

 

There are n cities. Each city has only one ball at the beginning, ranging from 1 to n. One is (t, a, B), which ships all the balls in the city where the first ball is located to the city where the second ball is located, and the other is (Q, a) query whether the ball a is transported to the city X. The city X has y balls, and the city a is transported to Z, and the output is x y z. Train of Thought: Check the set, maintain a sum of balls in the Union process, and maintain the transport count t in the find process.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <map>#include <utility>#include <queue>#include <stack>using namespace std;const int INF=1<<30;const double eps=1e-6;const int N = 10000;int fa[N],sum[N],t[N];int n,m;int find(int x){    if(fa[x]==x)        return x;    else    {        int tmp=find(fa[x]);        t[x]+=t[fa[x]];        return fa[x]=tmp;    }}void run(){    static int cas = 1;    printf("Case %d:\n",cas++);    char s[5];    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)        fa[i]=i,sum[i]=1,t[i]=0;    int u,v;    while(m--)    {        scanf("%s",s);        if(s[0]==‘T‘)        {            scanf("%d%d",&u,&v);            u=find(u);            v=find(v);            if(u!=v)            {                fa[u]=v;                sum[v]+=sum[u];                t[u]++;            }        }        else        {            scanf("%d",&u);            v=find(u);            printf("%d %d %d\n",v,sum[v],t[u]);        }    }}int main(){    freopen("case.txt","r",stdin);    int _;    scanf("%d",&_);    while(_--)        run();    return 0;}

 

HDU 3635 dragon bils (query 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.