(并查集+记录到根的距离)Dragon Balls--HDOJ

来源:互联网 发布:带制冰机的冰箱 知乎 编辑:程序博客网 时间:2024/05/16 00:40

Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 868 Accepted Submission(s): 344

Problem Description
Five hundred years later, the number of dragon balls will increase unexpectedly, so it\\\\’s too difficult for Monkey King(WuKong) to gather all of the dragon balls together.

His country has N cities and there are exactly N dragon balls 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) would be 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 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 balls 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 balls 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 balls in 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

2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1

Sample Output

Case 1:
2 3 0
Case 2:
2 2 1
3 3 2

Author
possessor WC

Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU

Recommend
lcy

思路:
题意:给出N个城市,每个城市一个龙珠,Q条指令,指令分为T转运,和Q查询,T A B,将A城市的龙珠运送到B城市,然后A,B这两个城市就相当于一个整体,例如在T A B的基础上再加一条指令TAC,单从这条指令上来看是讲A城市的龙珠运到C城市,根据题意,实际上是将B城市的龙珠运到C城市,很明显,这就是并查集了。
题目中还有一个问题就是,要你计算一个城市x到其所在集合根节点的距离,我们只需要在寻找x根节点的时候,计算出x到fa[x]的步数加上fa[x]到根节点的步数。

#include<iostream>#include<stdio.h>#include<algorithm>#include<string>#include<string.h>#include<math.h>using namespace std;int fa[10005];int sum[10005];int mov[10005];int step = 0,flg=0;int fid(int x){    if(x != fa[x])    {        int t = fa[x];        fa[x] = fid(fa[x]);        mov[x] += mov[t];//x到fa[x]的步数加上fa[x]到根节点的步数    }    return fa[x];}void mer(int x,int y){    int xx = fid(x);    int yy = fid(y);    if(xx != yy)    {        fa[xx] = yy;        sum[yy] += sum[xx];        mov[xx] = 1;// 由 fa[xx] = yy; 可知,xx 到 yy 只有一步    }}int main(void){ //   freopen("in.txt","r",stdin);    int ncase,num=1;    cin >> ncase;    while(ncase--)    {        int N,Q;        scanf("%d %d",&N,&Q);        printf("Case %d:\n",num++);        for(int i=1; i<=N; ++i)        {            fa[i] = i;            sum[i] = 1;            mov[i] = 0;        }        for(int i=0; i<Q; ++i)        {            char c[2];            int a,b,loc=-1,num=0;    //        getchar();            scanf("%s",c);    //        cin >> c;//用cin会超时            if(c[0] == 'T')            {                scanf("%d %d",&a,&b);                mer(a,b);            }            else            {                scanf("%d",&a);                step=0;                loc = fid(a);                printf("%d %d %d\n",loc,sum[loc],mov[a]);            }        }    }    return 0;}
原创粉丝点击