Dragon Balls

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

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

::挺不错的一道题,并查集,个人觉得维护某个球移动次数最难想到怎么去维护。

对于一个点的移动次数只要在合并的时候把该点移动次数加上其父亲的移动次数就好了。(想想,只有移动次数为0的球才能作为一个集合的“根”);

下面的代码思想是一样的,只是后一种用了点小技巧,省空间

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. const int N = ;
  7. int _, cas=, n, m, fa[N], num[N], shift[N];
  8.  
  9. void init()
  10. {
  11. for(int i=; i<=n; i++) fa[i]=i, num[i]=, shift[i]=;
  12. }
  13.  
  14. int find(int x)
  15. {
  16. if(x==fa[x]) return x;
  17. int p = fa[x];
  18. fa[x] = find(fa[x]);
  19. shift[x] += shift[p];
  20. return fa[x];
  21. }
  22.  
  23. void move_to(int u, int v)
  24. {
  25. u = find(u) , v =find(v);
  26. if(u==v) return ;
  27. fa[u] = v;
  28. num[v] += num[u];
  29. shift[u]++;
  30. }
  31.  
  32. void solve()
  33. {
  34. scanf("%d%d", &n, &m);
  35. init();
  36. char s[];
  37. int u, v;
  38. printf("Case %d:\n", cas++);
  39. while(m--)
  40. {
  41. scanf("%s%d", s, &u);
  42. if(s[]=='T'){
  43. scanf("%d", &v);
  44. move_to(u, v);
  45. }
  46. else{
  47. v =find(u);
  48. printf("%d %d %d\n", v, num[v], shift[u]);
  49. }
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. // freopen("in.txt", "r", stdin);
  56. cin>>_;
  57. while(_--) solve();
  58. return ;
  59. }

  1. view code#include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. const int N = 10010;
  7. int _, cas=1, n, m, fa[N], shift[N];
  8.  
  9. int find(int x)
  10. {
  11. if(fa[x]<0) return x;
  12. int p = fa[x];
  13. fa[x] = find(fa[x]);
  14. shift[x] += shift[p];
  15. return fa[x];
  16. }
  17.  
  18. void move_to(int u, int v)
  19. {
  20. u = find(u) , v =find(v);
  21. if(u==v) return ;
  22. fa[v] += fa[u];
  23. fa[u] = v;
  24. shift[u]++;
  25. }
  26.  
  27. void solve()
  28. {
  29. scanf("%d%d", &n, &m);
  30. for(int i=1; i<=n; i++) fa[i]=-1, shift[i]=0;
  31. char s[3];
  32. int u, v;
  33. printf("Case %d:\n", cas++);
  34. while(m--)
  35. {
  36. scanf("%s%d", s, &u);
  37. if(s[0]=='T'){
  38. scanf("%d", &v);
  39. move_to(u, v);
  40. }
  41. else{
  42. v =find(u);
  43. printf("%d %d %d\n", v, -fa[v], shift[u]);
  44. }
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. // freopen("in.txt", "r", stdin);
  51. cin>>_;
  52. while(_--) solve();
  53. return 0;
  54. }

hdu 3635 Dragon Balls(并查集)的更多相关文章

  1. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  2. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  5. hdu 3635 Dragon Balls (MFSet)

    Problem - 3635 切切水题,并查集. 记录当前根树的结点个数,记录每个结点相对根结点的转移次数.1y~ 代码如下: #include <cstdio> #include < ...

  6. hdu 3635 Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  8. hdu 3635 Dragon Balls(并查集)

    题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...

  9. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

随机推荐

  1. js验证电话号码的正则表达式

    在做程序时遇到js验证电话号码的问题,使用正则表达式来操作很简单的.一起来看一下吧. 1,这种是比较简单的验证号码: 电话号码只能包含”(“.“)”.“-”和数字 <input   type=t ...

  2. mysql学习笔记 第八天

    where,group by,having重新详解 where的用法: where与in的配合使用,in(值1,值2,...)表示结果在值1,值2,...其中任何一个. 聚合函数和group by的用 ...

  3. python爬虫——爬取NUS-WIDE数据库图片

    实验室需要NUS-WIDE数据库中的原图,数据集的地址为http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm   由于这个数据只给了每个图片的URL,所以需 ...

  4. Hibernate之lazy延迟加载(转)

    一.延迟加载的概念 当Hibernate从数据库中加载某个对象时,不加载关联的对象,而只是生成了代理对象,获取使用session中的load的方法(在没有改变lazy属性为false的情况下)获取到的 ...

  5. html button自动提交表单问题

    在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit,所以在chrome中,需要使用<button type="butt ...

  6. C# WM_NCMOUSELEAVE 消息触发

    public static extern bool TrackMouseEvent([In, Out] TRACKMOUSEEVENT lpEventTrack); [DllImport(" ...

  7. ORACLE 中ROWNUM用法总结!

    ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...

  8. seajs快速了解

    详情请点击原文     SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制.与jQuery等JavaScript框架不同,S ...

  9. ABAP中正则表达式的简单使用方法 (转老白BLOG)

    在一个论坛上面看到有人在问正则表达式的问题,特举例简单说明一下.另外,REPLACE也支持REGEX关键字.最后:只能是ECC6或者更高版本才可以(ABAP supports POSIX regula ...

  10. 使用Python给要素添加序号

    在ArcGIS的属性表中,由于编辑修改的原因,默认的FID或OID并不连续,经常需要给要素添加连读的序号,可使用Python代码完成. rec=-1 def autoIncrement(): glob ...