hdu 3635 Dragon Balls (带权并查集)

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3363    Accepted Submission(s): 1304

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

 题意:

      每一个城市都有一颗龙珠,但是随着时间的推移,龙珠会被移到其他的城市,悟空想去收集这些龙珠,但是他需要你告知他,他要找的那颗龙珠的所在的城市,以及这个城市所拥有的龙珠数量,还有这颗龙珠迁移过多少次。

代码:

 1 #define LOCAL
 2 #include<cstring>
 3 #include<cstdio>
 4 #define maxn 10005
 5 /*
 6     (1) 第i求所在的城市x  city[];
 7     (2)    x城市所拥有的球数量 ball[];
 8     (3)    i球被转移的次数     cnt[];
 9 */
10 int ball[maxn];
11 int city[maxn];
12 int cnt[maxn];
13 int n,q;
14  void init(){
15    for(int i=1 ; i<=n ;i++ ){
16          city[i]=i;
17          ball[i]=1;
18          cnt[i]=0; //转移次数
19    }
20  }
21 
22 //搜索该龙珠所在城市的位置
23 int  fin(int x)
24 {
25     //int tem;
26     if(x==city[x])
27       return city[x];
28     int tem=city[x];
29     city[x]=fin(city[x]);
30     cnt[x]+=cnt[tem];
31     return  city[x];
32 }
33 
34 void Union(int x,int y)
35 {
36     /*
37       将 x城市的所有龙珠转移到y城市中
38     */
39     x=fin(x);
40     y=fin(y);
41    if(x!=y){
42        city[x]=city[y];
43     ball[y]+=ball[x];
44     ball[x]=0;  //球全部移动到y城市中
45     cnt[x]=1;  //第一次移动
46    }
47 }
48 
49 int main()
50 {
51 
52   int t,a,b,tt=0;
53   char str[2];
54   scanf("%d",&t);
55   while(t--){
56    scanf("%d%d",&n,&q);
57    init();
58    printf("Case %d:\n",++tt);
59    while(q--){
60          scanf("%s",str);
61          if(str[0]=='T'){
62           scanf("%d%d",&a,&b);
63           Union(a,b);
64       }
65       else
66       {
67           scanf("%d",&a);
68             b=fin(a);
69             printf("%d %d %d\n",b,ball[b],cnt[a]);
70       }
71    }
72   }
73   return 0;
74 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏HansBug's Lab

3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队

3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 Time Limit: 3 Sec  Memory Limit: 12...

22950
来自专栏HansBug's Lab

3384/1750: [Usaco2004 Nov]Apple Catching 接苹果

3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec  Memory Limit: 1...

272110
来自专栏ml

hdu-----(4514)湫湫系列故事——设计风景线(树形DP+并查集)

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/3276...

39580
来自专栏小樱的经验随笔

HDU 2549 壮志难酬

壮志难酬 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java...

275100
来自专栏ml

hdu----(1847)Good Luck in CET-4 Everybody!(简单巴什博奕)

Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory L...

26740
来自专栏算法修养

浙江工业大学校赛 画图游戏

画图游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

31370
来自专栏ml

HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Lim...

35880
来自专栏HansBug's Lab

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit:...

34570
来自专栏潇涧技术专栏

Python Algorithms - C1 Introduction

算法导论是一本经典的大而全的算法书籍,而本书Python Algorithms不是来取代而是来补充算法导论的,因为算法导论提供的是简易的伪代码和详细的证明,而本...

9220
来自专栏数据结构与算法

POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)

Description Being a knight is a very attractive career: searching for the Holy G...

38470

扫码关注云+社区

领取腾讯云代金券