开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 
操作有三种: 
1 xx1 yy1 : 合并xx1与yy1两个集合 
2 xx1 yy1 :将xx1元素分离出来合到yy1上 
3 xx1 :查询xx1集合的元素个数,和元素所有值总和

  并查集,1就是合并两个集合,3要记录两个权值。因为只要祖先的权值,所以Find操作不需要更新权值。 
  接着就是分离元素了,在这儿我使用映射的方法:开始每个元素都映射自己,接着要删除元素时,我不直接删除元素(因为删除的话可能影响很大),我把此位置映射到不可能出现过得的新的一个值,这样我们处理一下原来的集合,再使用新的值维护一下现在的集合就好了,因为以后我们只是看映射的值,所以虽然没有直接删除值,但是原来的值我们不用。

  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<vector>
  7. #include<string>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<stdlib.h>
  11. #include<iostream>
  12. #include<algorithm>
  13. using namespace std;
  14. #define eps 1E-8
  15. /*注意可能会有输出-0.000*/
  16. #define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
  17. #define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
  18. #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
  19. #define mul(a,b) (a<<b)
  20. #define dir(a,b) (a>>b)
  21. typedef long long ll;
  22. typedef unsigned long long ull;
  23. const int Inf=<<;
  24. const double Pi=acos(-1.0);
  25. const int Mod=1e9+;
  26. const int Max=;
  27. int fat[Max],num[Max];
  28. ll ran[Max];
  29. int mp[Max],tot;//下标映射数字 找新值的代替前面的
  30. void Init(int n)
  31. {
  32. for(int i=;i<=n;i++)
  33. {
  34. fat[i]=i;
  35. ran[i]=(ll)i;
  36. num[i]=;
  37. mp[i]=i;
  38. }
  39. tot=n+;
  40. return;
  41. }
  42. int Find(int x)
  43. {
  44. if(x==fat[x])
  45. return fat[x];
  46. return fat[x]=Find(fat[x]);
  47. }
  48. void Union(int x,int y,int typ)
  49. {
  50. int prex=x;//注意保存原来的值
  51. x=mp[x],y=mp[y];//注意映射
  52. int x1=Find(x);
  53. int y1=Find(y);
  54. if(x1==y1)
  55. return;
  56. if(typ==)
  57. {
  58. fat[x1]=y1;
  59. ran[y1]+=ran[x1];
  60. num[y1]+=num[x1];
  61. return;
  62. }
  63. mp[prex]=tot++;//删除原有,添加到新地方,注意mp
  64.  
  65. fat[mp[prex]]=y1;
  66. num[mp[prex]]=;
  67. ran[mp[prex]]=(ll)x;
  68.  
  69. num[x1]--;
  70. ran[x1]-=(ll)prex;
  71.  
  72. num[y1]++;
  73. ran[y1]+=(ll)prex;
  74. return;
  75. }
  76. int main()
  77. {
  78. int n,m;
  79. int typ,xx1,yy1;
  80. while(~scanf("%d %d",&n,&m))
  81. {
  82. Init(n);
  83. for(int i=;i<m;i++)
  84. {
  85. scanf("%d",&typ);
  86. if(typ==)
  87. {
  88. scanf("%d %d",&xx1,&yy1);
  89. Union(xx1,yy1,);
  90. }
  91. else if(typ==)
  92. {
  93. scanf("%d %d",&xx1,&yy1);
  94. Union(xx1,yy1,);
  95. }
  96. else
  97. {
  98. scanf("%d",&xx1);
  99. yy1=Find(mp[xx1]);
  100. printf("%d %lld\n",num[yy1],ran[yy1]);
  101. }
  102. }
  103. }
  104. return ;
  105. }

UVA 11987 Almost Union-Find (并查集+删边)的更多相关文章

  1. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  2. HDU——2473Junk-Mail Filter(并查集删点)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  3. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  4. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  5. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  6. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

  7. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  8. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  9. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

随机推荐

  1. 【关于服务器端SQL Server 2008的设置】 使其他客户端机可通过ODBC数据源可访问

    服务器系统:Server 2003 数据库:SQL Server 2005 服务器配置:开启服务:server.workstation(这两个服务对于提供局域网共享有很大作用) 关闭防火墙 开启gue ...

  2. [转]Android中Application类的用法

    原文链接:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application ...

  3. Divide and conquer:Median(POJ 3579)

        快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...

  4. 【C语言】二维指针做形参

    转自:http://hi.baidu.com/gpmzccqceabimqq/item/f499f057aa1520404eff208b 关键: 传入时强制类型转换 + 使用时自己手工寻址 今天写程序 ...

  5. 【linux】sudo su切换到root权限

    在用户有sudo权限但不知道root密码时可用 sudo su切换到root用户

  6. LeetCode 459 Repeated Substring Pattern

    Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...

  7. Jquery选中行实现行中的Checkbox的选中与取消选中

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  8. 群内大神与你交流WEB经验 业内专家指点就职技巧

    就知道你是一个有理想要抱负的人,不会满足于做一个初级的前端开发工程师.在接下来的这个阶段,我们将走上前端开发的进阶之路,将自己的能力再往上拔高一个等级.同样,薪资也会往上升一个等级!但是,如果你是一个 ...

  9. Runtime.getRuntime().exec()

    Runtime.getRuntime()返回当前应用程序的Runtime对象,该对象 的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实 ...

  10. 设计模式学习之代理模式(Proxy,结构型模式)(11)

    参考地址:http://www.cnblogs.com/zhili/p/ProxyPattern.html 一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访 ...