http://acm.hdu.edu.cn/showproblem.php?pid=3635

题目意思是说n个球在n个城市。

每次操作把编号i的球所在的城市的所有的求全部一道另一城市B

每次询问访问编号i的球的城市,以及这个城市的球的数量,以及这个球被移动了多少次。

方法就是用一个结构体记录每个球的父节点和移动次数以及这个节点的球的个数(球和城市可以堪为一个整体),然后每次操作就行。

  1. #include <map>
  2. #include <set>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cctype>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define MAX(a,b) (a > b ? a : b)
  17. #define MIN(a,b) (a < b ? a : b)
  18. #define mem0(a) memset(a,0,sizeof(a))
  19. #define lson k<<1, L, mid
  20. #define rson k<<1|1, mid+1, R
  21.  
  22. typedef long long LL;
  23. const double eps = 1e-;
  24. const int MAXN = ;
  25. const int MAXM = ;
  26.  
  27. struct NODE
  28. {
  29. int num;
  30. int fa;
  31. int time;
  32. }p[MAXN];
  33. int N, Q;
  34.  
  35. void init()//初始化
  36. {
  37. for(int i=;i<=N;i++) {
  38. p[i].fa = i;
  39. p[i].num = ;
  40. p[i].time = ;
  41. }
  42. }
  43.  
  44. int find(int x)//找父节点
  45. {
  46. if(x == p[x].fa) return x;
  47. int fa = p[x].fa;//记录这个节点的父节点
  48. p[x].fa= find(p[x].fa);
  49. p[x].time += p[fa].time;//这个节点移动的次数加上他父节点次数
  50. return p[x].fa;
  51. }
  52.  
  53. void Union(int x, int y)//合并
  54. {
  55. int a = find(x);
  56. int b = find(y);
  57. if(a != b)
  58. {
  59. p[a].fa = p[b].fa;//连边
  60. p[b].num += p[a].num;//龙珠的数量
  61. p[a]. time ++;//a又多移动了一次
  62. }
  63. }
  64.  
  65. int main()
  66. {
  67. //freopen("in.txt", "r", stdin);
  68. int T=, Cas;
  69. scanf("%d", &Cas);
  70. while(Cas--)
  71. {
  72. scanf("%d%d%*c", &N, &Q);
  73. init();
  74. char ch; int a, b;
  75. printf("Case %d:\n", ++T);
  76. for(int i=;i<Q;i++)
  77. {
  78. scanf("%c", &ch);
  79. if(ch == 'T') {
  80. scanf("%d %d%*c", &a, &b);
  81. Union(a, b);
  82. }
  83. else {
  84. scanf("%d%*c", &a);
  85. b = find(a);//必须先找一遍他被移动了多少次
  86. printf("%d %d %d\n", b, p[b].num, p[a].time);
  87. }
  88. }
  89. }
  90. return ;
  91. }

但是我把树的层数减少那步去掉后字节搜索层数居然和这个没什么区别,只能说数据太弱了= =

  1. #include <map>
  2. #include <set>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cctype>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define MAX(a,b) (a > b ? a : b)
  17. #define MIN(a,b) (a < b ? a : b)
  18. #define mem0(a) memset(a,0,sizeof(a))
  19. #define lson k<<1, L, mid
  20. #define rson k<<1|1, mid+1, R
  21.  
  22. typedef long long LL;
  23. const double eps = 1e-;
  24. const int MAXN = ;
  25. const int MAXM = ;
  26.  
  27. struct NODE
  28. {
  29. int num;
  30. int fa;
  31. }p[MAXN];
  32. int N, Q;
  33.  
  34. void init()
  35. {
  36. for(int i=;i<=N;i++) {
  37. p[i].fa = i;
  38. p[i].num = ;
  39. }
  40. }
  41.  
  42. int find(int x)
  43. {
  44. if(x == p[x].fa) return x;
  45. return find(p[x].fa);
  46. }
  47.  
  48. void Union(int x, int y)
  49. {
  50. int a = find(x);
  51. int b = find(y);
  52. if(a != b)
  53. {
  54. p[a].fa = b;
  55. p[b].num += p[a].num;
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. int T=, Cas;
  62. scanf("%d", &Cas);
  63. while(Cas--)
  64. {
  65. scanf("%d%d%*c", &N, &Q);
  66. init();
  67. char ch; int a, b;
  68. printf("Case %d:\n", ++T);
  69. for(int i=;i<Q;i++)
  70. {
  71. scanf("%c", &ch);
  72. if(ch == 'T') {
  73. scanf("%d %d%*c", &a, &b);
  74. Union(a, b);
  75. }
  76. else {
  77. int time = ;
  78. scanf("%d%*c", &a);
  79. while(a != p[a].fa){ time ++; a = p[a].fa; }
  80. printf("%d %d %d\n", a, p[a].num, time);
  81. }
  82. }
  83. }
  84. return ;
  85. }

HDU3635Dragon Balls(并查集)的更多相关文章

  1. hdu 3635 Dragon Balls(并查集)

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

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

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

  3. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  4. hdu3635 Dragon Balls(带权并查集)

    /* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...

  5. [HDOJ3635]Dragon Balls(并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...

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

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

  7. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  8. ZOJ3761(并查集+树的遍历)

    Easy billiards Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Edward think a g ...

  9. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

  10. WUSTOJ 1319: 球(Java)并查集

    题目链接:1319: 球 参考:wustoj 1319 球-wust_tanyao,并查集 并查集系列:WUSTOJ 1346: DARK SOULS(Java)并查集 Description Icy ...

随机推荐

  1. 51nod1437 迈克步

    傻叉单调栈 #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> ...

  2. $^,$@,$?,$<,$(@D),$(@F) of makefile

    makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义 $(filter-out $(PHONY) $(wildcard $^),$^)常用 ...

  3. HDU 5264 pog loves szh I (字符串,水)

    题意:设有两个串A和B,现将B反转,再用插入的形式合成一个串.如:A:abc   B:efg:反转B先,变gfe:作插入,agbfce.现在给出一个串,要求还原出A和B. 思路:扫一遍O(n),串A在 ...

  4. swun 1388 你的背包

    解题思路:这题给人的第一反应是背包,第二反应是贪心,我用的是搜索,枚举就可以,要有这种意识, 题目数据只有8个,暴力是可以解决的. #include<cstdio> #include< ...

  5. uva 11752 - The Super Powers

    这个题   任意一个数,他的幂只要不是质数则可以分解成两个数的乘   判断有没有溺出  i×i  则用最大的那个数 Max/i < i 吗 #include<iostream> #i ...

  6. 虚拟机安装centos 6 报错Erro processing drive

    错误提示: Error processing drive: pci-0000:00:10-scsi-0:0:0:0 20480MB VMware,VMware Virtual S This devic ...

  7. Vagrant使用笔记

    vagrant box add [options] <name, url, or path> - 添加box至vagrant的管理列表 vagrant init 初始化虚拟机至当前文件夹并 ...

  8. Android RecyclerView使用详解(二)

    在上一篇(RecyclerView使用详解(一))文章中简单的介绍了RecyclerView的基本用法,接下来要来讲讲RecyclerView的更多用法,要实现不同的功能效果,大部分都还是在于Recy ...

  9. Android之Socket群组聊天

    在这只做了一个简单的例子,没有用到数据库,思路就是客户端发送信息到服务器端,服务器端转发所有数据到客户端,校验服务器端发来消息是否是自己发出的,如果是自己发出的,则不显示自己的消息 贴一下Androi ...

  10. 【转】Undefined symbols for architecture i386:和"_OBJC_CLASS_$_xx", referenced from:问题解决方法

    多个人共同操作同一个项目或拷贝项目时,经常会出现类似这样的问题: Undefined symbols for architecture i386: "_OBJC_CLASS_$_xx文件名& ...