hdu 5.1.6 Dragon Balls

来源:互联网 发布:高邮淘宝美工培训机构 编辑:程序博客网 时间:2024/06/06 04:58

二专明天考试。。啊哈哈……水了。

好吧……这不是不去做题的借口……毕竟我明明还有时间刷人人的- -。。。还有闲情去调戏队长大人的。。。>_<~~~

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 100 Accepted Submission(s): 59 
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
23 3T 1 2T 3 2Q 23 4T 1 2Q 1T 1 3Q 1
 
Sample Output
Case 1:2 3 0Case 2:2 2 13 3 2
 

唔……hdu暂时不能上acmstep……明天来更吧。

话说难道是因为我昨天木好好做题今天才悲剧的又被拒了……么……

填坑来了……不过……代码是抄的……cnt那里怎么都理解不能,后来自己略改了一下,也过了,发现大概明白了,不过自己想这个难度就……

姑且就这样吧……

//第a个龙珠所在的城市与这个城市有多少颗龙珠比较简单//用并查集找到结点a的根结点与根结点的秩就行了,//而龙珠a被移动的次数比较麻烦,//我们将合并两个集合的过程看做第一个集合的根结点移动次数+1,//那对于任意一个结点就可以用cnt[a]+cnt[find(a)]表示a结点移动的次数,//在合并的时候由于根结点改变,所以要更新cnt的值。
//由于cnt的值在不断的修改,所以cnt[find(a)]总为零……所以可不加。#include <iostream>using namespace std;int root[10002],number[10002],cnt[10002];int find(int x){    if(x!=root[x])    {        int t=root[x];        root[x]=find(root[x]);        cnt[x]+=cnt[t];    }    return root[x];}void merge(int a,int b){     int fa=find(a);     int fb=find(b);     root[fa]=fb;     number[fb]+=number[fa];     cnt[fa]++;}     int main(){    int cas;    cin>>cas;    int ci;    for(ci=1;ci<=cas;ci++)    {        int n,q;        cin>>n>>q;        cout<<"Case "<<ci<<":"<<endl;        int i;        for(i=0;i<10002;i++)        {            root[i]=i;            number[i]=1;            cnt[i]=0;        }        while(q--)        {            char a[2];            scanf("%s",a);            if(a[0]=='T')            {                int begin,end;                scanf("%d%d",&begin,&end);                merge(begin,end);            }            else            {                int num;                scanf("%d",&num);                printf("%d %d %d\n",find(num),number[find(num)],cnt[num]);            }        }    }    system("pause");    return 0;}


原创粉丝点击