1. 题目描述
  2. Its universally acknowledged that therere innumerable trees in the campus of HUST.
  3. Thus a professional tree manager is needed. Your task is to write a program to help manage the trees.
  4. Initially, there are n forests and for the i-th forest there is only the i-th tree in it. Given four kinds of operations.
  5. 1 u v, merge the forest containing the u-th tree and the forest containing the v-th tree;
  6. 2 u, separate the u-th tree from its forest;
  7. 3 u, query the size of the forest which contains the u-th tree;
  8. 4 u v, query whether the u-th tree and the v-th tree are in the same forest.
  9. 输入描述:
  10. The first line contains an integer T, indicating the number of testcases.
  11. In each test case:
  12. The first line contains two integers N and Q, indicating the number of initial forests and the number of operations.
  13. Then Q lines follow, and each line describes an operation.
  14. 输出描述:
  15. For each test cases, the first line should be "Case #i:", where i indicate the test case i.
  16. For each query 3, print a integer in a single line.
  17. For each query 4, print "YES" or "NO" in a single line.
  18. 示例1
  19. 输入
  20. 1
  21. 10 8
  22. 3 1
  23. 4 1 2
  24. 1 1 2
  25. 3 1
  26. 4 1 2
  27. 2 1
  28. 3 1
  29. 4 1 2
  30. 输出
  31. Case #1:
  32. 1
  33. NO
  34. 2
  35. YES
  36. 1
  37. NO

题意:对trees有四种操作。

1 u v,将包含u-th树的森林和含有v-th树的森林合并在一起;

u,将u-th树与森林分开;

3 u,查询包含u-th树的森林的大小;

4 u v,查询u-th树和v-th树是否在同一林中。

【分析】:给每个点都给他们造一个编号w,删除点,就相当于该点的编号w改变就可以了,也是相当于构造新点。

【出处】:UVA 11987 (白书267)

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<map>
  6. #include<string>
  7. #include<cstdlib>
  8. #include<cmath>
  9. #include<cstring>
  10. #include<set>
  11. #define LL long long
  12. #define INF 0xffffff
  13. using namespace std;
  14. const double pi = 2 * acos (0.0);
  15. const int maxn = 2e5+10;
  16. int n,m;
  17. int p[maxn];
  18. int s[maxn];
  19. int w[maxn];
  20. void init()
  21. {
  22. for(int i=1;i<=maxn;i++) //这里必须是maxn,其他的好像不行
  23. {
  24. p[i] = i;
  25. w[i] = i;
  26. s[i] = 1;
  27. }
  28. }
  29. int Find(int x)
  30. {
  31. return x==p[x]?x:p[x]=Find(p[x]);
  32. }
  33. void Union(int x,int y)
  34. {
  35. int fx = Find(x);
  36. int fy = Find(y);
  37. if(fx!=fy)
  38. {
  39. p[fx] = fy;
  40. s[fy] += s[fx];
  41. //printf("size=%d\n",s[fy]);
  42. }
  43. }
  44. int main()
  45. {
  46. int T;
  47. cin >> T;
  48. int k =1;
  49. while(T--)
  50. {
  51. printf("Case #%d:\n",k++);
  52. init();
  53. cin >> n >> m;
  54. int cnt = n;
  55. while(m--)
  56. {
  57. int op,u,v;
  58. scanf("%d",&op);
  59. if(op==1)
  60. {
  61. scanf("%d%d",&u,&v);
  62. Union(w[u],w[v]);
  63. }
  64. else if(op==2)
  65. {
  66. scanf("%d",&u);
  67. s[Find(w[u])]--;
  68. w[u] = ++cnt;
  69. }
  70. else if(op==3)
  71. {
  72. scanf("%d",&u);
  73. printf("%d\n",s[Find(w[u])]);
  74. }
  75. else{
  76. scanf("%d%d",&u,&v);
  77. if(Find(w[u])==Find(w[v]))
  78. printf("YES\n");
  79. else printf("NO\n");
  80. }
  81. }
  82. }
  83. return 0;
  84. }

第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  2. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...

  3. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  4. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  5. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  6. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  7. 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest

    链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...

  8. 第十四届华中科技大学程序设计竞赛--J Various Tree

    链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  9. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

随机推荐

  1. 4G来临,短视频社交分享应用或井喷

    因为工作的原因,接触短视频社交应用的时间相对较多,不管是自家的微视,还是别人家的Vine.玩拍.秒拍等,都有体验过.随着时间的推移,我愈发感受到有一股似曾相识的势能正在某个地方慢慢积聚,直到今天我才猛 ...

  2. 使用Unity做项目的时候,一些好的建议

    内容来自这个网站http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/ ,我选取了目前我看得懂的一 ...

  3. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  4. 玩转Openstack之Nova中的协同并发(一)

    玩转Openstack之Nova中的协同并发(一) 前不久参加了个Opnstack的Meetup,其中有一个来自EasyStack的大大就Nova中的协同并发做了一番讲解,有所感触,本想当天就总结一下 ...

  5. securecrt切换会话(session)的显示方式

    Window(窗口)-> Tabs(选项卡)/Tile Vertically(垂直平铺)/Tile Horizontally(水平平铺)/Cascade(瀑布,如下图效果)

  6. Python urllib模块详解

    在Python 2中,有urllib和urllib2两个库来实现请求的发送.而在Python 3中,已经不存在urllib2这个库了,统一为urllib,其官方文档链接为:https://docs.p ...

  7. selenium 使用谷歌浏览器模拟wap测试

    /** * 使用谷歌浏览器模拟手机浏览器 * @param devicesName * @author xxx * 创建时间:2017-06-15,更新时间:2017-06-15 * 备注 */ pu ...

  8. 【转载】Unity3D研究院之静态自动检查代码缺陷与隐患

    代码缺陷和代码错误的最大区别是,代码缺陷不影响游戏编译,而代码错误编译都不通过.但是代码缺陷会影响游戏发布后产生的一系列BUG..我今天无意间逛外国论坛发现的一个方法,使用了一下感觉挺给力的第一时间分 ...

  9. UVALive 6324 Archery (求射箭覆盖的期望)

    #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> const d ...

  10. restorator 运行后其他所有EXE文件都无法运行的解决方案

    昨天要反编译一个EXE,用RESTORATOR来查看资源罗列情况,倒霉的事情发生了,所有EXE文件点右键后‘打开’都没有了,刚开始以为中度了,进安全模式看,发现文件都没有异常,并且在安全模式下问题照样 ...