1.并查集的定义

在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题。有一个联合-查找算法(union-find algorithm)定义了两个用于此数据结构的操作:

  • Find:确定元素属于哪一个子集。

  • Union:将两个子集合并成同一个集合。

  • MakeSet,用于建立单元素集合。

为了更加精确的定义这些方法,需要定义如何表示集合。一种常用的策略是为每个集合选定一个固定的元素,称为代表,以表示整个集合。接着,Find(x) 返回x所属集合的代表,而Union使用两个集合的代表作为参数。

并查集森林是一种将每一个集合以树表示的数据结构,其中每一个节点保存着到它的父节点的引用

比如上面是两个集合分别记为A和B,集合A有4个元素,集合B有3个元素,每个元素都只对应一个集合,也就是一对一,对于集合的表示,通常都是使用根节点来判断,也就是说,1和5分别表示两个集合

  1. //集合1的元素
  2. 1,2,3,4
  3. //集合5的元素
  4. 5,6,7

那么

  1. find(1)=1
  2. find(2)=1
  3. find(3)=1
  4. find(4)=1
  5. find(5)=5
  6. find(6)=5
  7. find(7)=5

2.并查集的基本操作

2.1并查集的存储结构

并查集通常用一个数组来表示,

  1. int father[N];

其中father[i]表示元素i的父亲节点,而父亲节点本身也是这个集合内的元素,判断一个元素的集合代表,就是一直遍历元素的父亲节点,一直到father[i]=i,表示元素i的父亲节点是它自己,则遍历结束,用这个元素i来表示这个集合。

2.2并查集的初始化

一开始,每一个元素都是一个独立的集合,那么这个元素本身就可以代表集合,所以需要令father[i]等于i(1<=i<=N)。

  1. for(int i = 1; i<= N;i++){
  2. father[i] = i;
  3. }

2.3查找

查找一个元素所在的集合就是查找树的根节点,可以通过递归或者迭代来实现。

递归查找

  1. int find(int x){
  2. if(x == father[x]) return x;
  3. else return find(father[x]);
  4. }

迭代查找

  1. int find(int x){
  2. while(x != father[x]){
  3. x = father[x];
  4. }
  5. return x;
  6. }

2.4合并

合并操作是对两个不同的集合合并成一个集合,接收两个元素,判断两个元素是否在不同的集合上,然后将其中一个集合的根节点的父亲结点指向另一个集合的根节点即可。

  1. void union(int a, int b){
  2. int faA = find(a);
  3. int fab = find(b);
  4. if(faa != fab){
  5. father[faa] = fab;
  6. }
  7. }

3.并查集的优化

存在一种极端情况,树的逻辑结构是斜二叉树,也就是树退化成了单链表,每次查找的时候时间复杂度都是O(n)。一种优化思路是,把每一个元素的父亲节点直接指向根节点。

  1. int find(int x){
  2. int a = x;
  3. while(x != father[x]){
  4. x = father[x];
  5. }
  6. while(a != father[a]){
  7. int z = a;
  8. a = father[a];
  9. father[z] = x;
  10. }
  11. return x;
  12. }

4.并查集的应用

以下是PAT的三道关于并查集的问题。

1107 Social Clusters

When register on a social network, you are always asked to specify your hobbies in order to find some

potential friends with the same hobbies. A “social cluster” is a set of people who have some of their

hobbies in common. You are supposed to find all the clusters.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N

(<=1000), the total number of people in a social network. Hence the people are numbered from 1 to N.

Then N lines follow, each gives the hobby list of a person in the format:

Ki: hi[1] hi[2] … hi[Ki]

where Ki (>0) is the number of hobbies, and hi[j] is the index of the j-th hobby, which is an integer in [1,

1000].

Output Specification:

For each case, print in one line the total number of clusters in the network. Then in the second line, print

the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly

one space, and there must be no extra space at the end of the line.

Sample Input:

8

3: 2 7 10

1: 4

2: 5 3

1: 4

1: 3

1: 4

4: 6 8 1 5

1: 4

Sample Output:

3

4 3 1

题目大意:有n个人,每个人喜欢k个活动,如果两个人有任意一个活动相同,就称为他们处于同一个社交网络。求这n个人一共形成了多少个社交网络。

需要注意的是如果A和B组成了一个集合,B和C组成了一个集合,那么A和B、C应该是在一个集合中的,尽管A和C并没有相同的活动

这道题是一道很经典的并查集问题,每个人喜欢的活动数目都不一样,根据活动来判断两个人是否属于同一个集合,每个人的编号就是集合的元素,编号从1开始到n,如果是同一个集合,则合并,最后遍历father数组的每一个元素,然后查找每一个元素的根节点,根据根节点来记录每一个集合的人数,最后统计这些集合个数即可。

  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. vector<int> father, isRoot;
  6. int cmp1(int a, int b) {
  7. return a > b;
  8. }
  9. int findFather(int x) {
  10. int a = x;
  11. while(x != father[x])
  12. x = father[x];
  13. while(a != father[a]) {
  14. int z = a;
  15. a = father[a];
  16. father[z] = x;
  17. }
  18. return x;
  19. }
  20. void Union(int a, int b) {
  21. int faA = findFather(a);
  22. int faB = findFather(b);
  23. if(faA != faB) father[faA] = faB;
  24. }
  25. int main() {
  26. int n, k, t, cnt = 0;
  27. int course[1001] = {0};
  28. scanf("%d", &n);
  29. father.resize(n + 1);
  30. isRoot.resize(n + 1);
  31. for(int i = 1; i <= n; i++)
  32. father[i] = i;
  33. for(int i = 1; i <= n; i++) {
  34. scanf("%d:", &k);
  35. for(int j = 0; j < k; j++) {
  36. scanf("%d", &t);
  37. if(course[t] == 0)
  38. course[t] = i;
  39. Union(i, findFather(course[t]));
  40. }
  41. }
  42. for(int i = 1; i <= n; i++)
  43. isRoot[findFather(i)]++;
  44. for(int i = 1; i <= n; i++) {
  45. if(isRoot[i] != 0) cnt++;
  46. }
  47. printf("%d\n", cnt);
  48. sort(isRoot.begin(), isRoot.end(), cmp1);
  49. for(int i = 0; i < cnt; i++) {
  50. printf("%d", isRoot[i]);
  51. if(i != cnt - 1) printf(" ");
  52. }
  53. return 0;
  54. }

1114 Family Property

This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:

  1. ID` `Father` `Mother` k Child1⋯Childk Mestate Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID's of this person's parents (if a parent has passed away, -1 will be given instead); k (0≤k≤5) is the number of children of this person; Child**i's are the ID's of his/her children; Mestate is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

ID M AVGsets AVGarea

where ID is the smallest ID in the family; M is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID's if there is a tie.

Sample Input:

  1. 10
  2. 6666 5551 5552 1 7777 1 100
  3. 1234 5678 9012 1 0002 2 300
  4. 8888 -1 -1 0 1 1000
  5. 2468 0001 0004 1 2222 1 500
  6. 7777 6666 -1 0 2 300
  7. 3721 -1 -1 1 2333 2 150
  8. 9012 -1 -1 3 1236 1235 1234 1 100
  9. 1235 5678 9012 0 1 50
  10. 2222 1236 2468 2 6661 6662 1 300
  11. 2333 -1 3721 3 6661 6662 6663 1 100

Sample Output:

  1. 3
  2. 8888 1 1.000 1000.000
  3. 0001 15 0.600 100.000
  4. 5551 4 0.750 100.000
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. struct DATA {
  5. int id, fid, mid, num, area;
  6. int cid[10];
  7. } data[1005];
  8. struct node {
  9. int id, people;
  10. double num, area;
  11. bool flag = false;
  12. } ans[10000];
  13. int father[10000];
  14. bool visit[10000];
  15. int find(int x) {
  16. while(x != father[x])
  17. x = father[x];
  18. return x;
  19. }
  20. void Union(int a, int b) {
  21. int faA = find(a);
  22. int faB = find(b);
  23. if(faA > faB)
  24. father[faA] = faB;
  25. else if(faA < faB)
  26. father[faB] = faA;
  27. }
  28. int cmp1(node a, node b) {
  29. if(a.area != b.area)
  30. return a.area > b.area;
  31. else
  32. return a.id < b.id;
  33. }
  34. int main() {
  35. int n, k, cnt = 0;
  36. scanf("%d", &n);
  37. for(int i = 0; i < 10000; i++)
  38. father[i] = i;
  39. for(int i = 0; i < n; i++) {
  40. scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid, &k);
  41. visit[data[i].id] = true;
  42. if(data[i].fid != -1) {
  43. visit[data[i].fid] = true;
  44. Union(data[i].fid, data[i].id);
  45. }
  46. if(data[i].mid != -1) {
  47. visit[data[i].mid] = true;
  48. Union(data[i].mid, data[i].id);
  49. }
  50. for(int j = 0; j < k; j++) {
  51. scanf("%d", &data[i].cid[j]);
  52. visit[data[i].cid[j]] = true;
  53. Union(data[i].cid[j], data[i].id);
  54. }
  55. scanf("%d %d", &data[i].num, &data[i].area);
  56. }
  57. for(int i = 0; i < n; i++) {
  58. int id = find(data[i].id);
  59. ans[id].id = id;
  60. ans[id].num += data[i].num;
  61. ans[id].area += data[i].area;
  62. ans[id].flag = true;
  63. }
  64. for(int i = 0; i < 10000; i++) {
  65. if(visit[i])
  66. ans[find(i)].people++;
  67. if(ans[i].flag)
  68. cnt++;
  69. }
  70. for(int i = 0; i < 10000; i++) {
  71. if(ans[i].flag) {
  72. ans[i].num = (double)(ans[i].num * 1.0 / ans[i].people);
  73. ans[i].area = (double)(ans[i].area * 1.0 / ans[i].people);
  74. }
  75. }
  76. sort(ans, ans + 10000, cmp1);
  77. printf("%d\n", cnt);
  78. for(int i = 0; i < cnt; i++)
  79. printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num,
  80. ans[i].area);
  81. return 0;
  82. }

1118 Birds in Forest

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤10

^4 ) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B1 B2 ... BK

where K is the number of birds in this picture, and Bi 's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 10^4

After the pictures there is a positive number Q (≤10^4 ) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:

For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line Yes if the two birds belong to the same tree, or No if not.

Sample Input:

4

3 10 1 2

2 3 4

4 1 5 7 8

3 9 6 4

2

10 5

3 7

Sample Output:

2 10

Yes

No

  1. #include <iostream>
  2. using namespace std;
  3. int n, m, k;
  4. const int maxn = 10010;
  5. int fa[maxn] = {0}, cnt[maxn] = {0};
  6. int findFather(int x) {
  7. int a = x;
  8. while(x != fa[x])
  9. x = fa[x];
  10. while(a != fa[a]) {
  11. int z = a;
  12. a = fa[a];
  13. fa[z] = x;
  14. }
  15. return x;
  16. }
  17. void Union(int a, int b) {
  18. int faA = findFather(a);
  19. int faB = findFather(b);
  20. if(faA != faB) fa[faA] = faB;
  21. }
  22. bool exist[maxn];
  23. int main() {
  24. scanf("%d", &n);
  25. for(int i = 1; i <= maxn; i++)
  26. fa[i] = i;
  27. int id, temp;
  28. for(int i = 0; i < n; i++) {
  29. scanf("%d%d", &k, &id);
  30. exist[id] = true;
  31. for(int j = 0; j < k-1; j++) {
  32. scanf("%d", &temp);
  33. Union(id, temp);
  34. exist[temp] = true;
  35. }
  36. }
  37. for(int i = 1; i <= maxn; i++) {
  38. if(exist[i] == true) {
  39. int root = findFather(i);
  40. cnt[root]++;
  41. }
  42. }
  43. int numTrees = 0, numBirds = 0;
  44. for(int i = 1; i <= maxn; i++) {
  45. if(exist[i] == true && cnt[i] != 0) {
  46. numTrees++;
  47. numBirds += cnt[i];
  48. }
  49. }
  50. printf("%d %d\n", numTrees, numBirds);
  51. scanf("%d", &m);
  52. int ida, idb;
  53. for(int i = 0; i < m; i++) {
  54. scanf("%d%d", &ida, &idb);
  55. printf("%s\n", (findFather(ida) == findFather(idb)) ? "Yes" : "No");
  56. }
  57. return 0;
  58. }

【algo&ds】【pat】5.并查集及其应用的更多相关文章

  1. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

  2. PAT A1107——并查集

     Social Clusters When register on a social network, you are always asked to specify your hobbies in ...

  3. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  4. PAT A1107 Social Clusters (30 分)——并查集

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  5. PAT A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  6. PAT L2-013 红色警报(并查集求连通子图)

    战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...

  7. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  8. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

  9. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

随机推荐

  1. Vmare 无法打开内核设备“\\.\VMCIDev\VMX”: 系统找不到指定的文件。您在安装 VMware Workstation 后是否进行了重新引导?的解决办法

    1.使用管理员省份运行cmd:net start vmx86(切记是要用管理员身份),启动服务成功问题即可解决. 2.若1操作中启动失败,则到Vmare安装目录下搜索vmx86.sys文件,并拷贝到C ...

  2. 你不知道的JavaScript(中)读书笔记(二)

    第三章 原生函数 常用的原生函数(内建函数)有: String() Number() Boolean Array() Object() Function() RegExp() Date() Erroe ...

  3. 安装完PyCharm,启动时弹出Failed to load JVM DLLbinserverjvm

    安装完PyCharm,启动时弹出"Failed to load JVM DLL\bin\server\jvm.dll"解决方案 问题描述:打开PyCharm时,弹出"Fa ...

  4. Linux下监控用户操作轨迹

    在实际工作当中,都会碰到误删除.误修改配置文件等事件.如果没有堡垒机,要在linux系统上查看到底谁对配置文件做了误操作,特别是遇到删库跑路的事件,当然可以通过history来查看历史命令记录,但如果 ...

  5. python GUI编程tkinter示例之目录树遍历工具

    摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...

  6. GIS面试小知识点

    1.什么是地理信息系统?简述其基本功能 它是随着地理科学.计算机技术.遥感技术和信息科学的发展而产生的一门科学.就应用而言,是对空间数据进行  组织.管理.分析.显示  的系统.其实本质上它探讨的就是 ...

  7. JDK1.8新特性-Lambda表达式

    Lambda 表达式 Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性. Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中). 使用 Lambda 表 ...

  8. 【MySQL】LIMIT以及LIMIT OFFSET

    LIMIT两种方法: 两种方法: ()LIMIT A; #表示从第一条记录开始取A条记录: ()LIMIT A,B; #参数A为可选参数,表示跳过A条数据(默认为0) #参数B为必选参数,表示取B行数 ...

  9. python-基础r/R、b、u/U含义

    1.r/R,代表非转义的原始字符串,一般使用在正则表达式和win目录上 2.b“” 代表b后面的内容为bytes类型 3.u/U 表示对字符串进行unicode编码,一般使用在有中午的地方,防止乱码.

  10. 安装最新版 windows正版软件地址(visio,office)

    链接地址为 https://msdn.itellyou.cn/ 进入后直接搜 然后复制链接使用迅雷下载 很快完成 但是都是原生的 需要破解 提供一个visio的破解软件 亲测有效 链接:https:/ ...