FJUT ACM 1216 Dragon Balls

Dragon Balls

TimeLimit: 2000/1000 MS (Java/Others)  MemoryLimit: 32768/32768 K (Java/Others)
64-bit integer IO format:%I64d
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.
SampleInput
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
SampleOutput
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
[思路]:这题是一题并查集,比较需要注意的是关于这个城市有几个球,球动了几次,是两个处理点,第一个有几个球是很好处理的,利用一个数组储存一开始n个城市每个城市有1个
球,再进行移动操作的时候,再加起来,把后者变成0.到时候查询城市,只需查询并查集的代表元素就可以了,而球的移动次数如何算了,我是使用了你连接的上级有几个,你就移动
几次的特点,因为不压缩路径的话,你每移动一次都会多一个上级,只要一直往上查询,直到查询到第一个元素,统计step就可以得出移动几次。
附上代码:
这题还要注意下用cin读入会TLE
用scanf就ac了
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAXN 1000005
int pre[MAXN];
int presum[MAXN];///这个城市有多少球
int unionsearch(int root)
{
    int son,tmp;
    son=root;
    while(root!=pre[root])///寻找父节点,一层一层向上
    {
     root=pre[root];
    }
    /**while(root!=son)///路径压缩,就是先将原上级保持在tmp内,在把pre[son]指向父节点,然后再将tmp赋值给son
    {tmp=pre[son];
    pre[son]=root;///这个过程会不断的去压缩路径,来优化,防止算法退化为O(N)的查找
    son=tmp;
    }///其实可以以递归写
    /**
    来至CWL学长的代码:
    int acfind(int x)
    {
    return pre[x]==x?x:pre[x]=acfind(pre[x]);
    }
    **/
    return root;
}
int unionfinds(int root)
{
    int son,tmp;
    int step=0;
    son=root;
    while(root!=pre[root])///寻找父节点,一层一层向上
    {
     root=pre[root];
     step++;
    }
    return step;
}
void unionjoin(int x,int y)///把x城市的球运到y城市
{
    int a,b;
    a=unionsearch(x);
    b=unionsearch(y);
    if(a!=b)
    {
        pre[a]=b;
        presum[b]+=presum[a];
        presum[a]=0;
    }
}
int main()
{
   int t,n,q;
   char a;
   int b,c;
   while(~scanf("%d",&t))
   {
       for(int i=0;i<t;i++)
       {
           scanf("%d%d",&n,&q);
           for(int j=1;j<=n;j++)///实现n个城市有1个球
            presum[j]=1;
           for(int j=1;j<=n;j++)///实现单个城市为单个集合
           {
               pre[j]=j;
           }
           printf("Case %d:\n",i+1);
           for(int j=0;j<q;j++)
           {getchar();
               scanf("%c",&a);
               if(a=='T')
                {scanf("%d%d",&b,&c);
                unionjoin(b,c);
                }
               if(a=='Q')
                {scanf("%d",&b);
                int d=unionsearch(b);
                printf("%d %d %d\n",d,presum[d],unionfinds(b));
                }
           }
       }
   }
}

 

posted @ 2018-03-02 17:24  moxin0509  阅读(331)  评论(0编辑  收藏  举报