并查集适用问题举例
- 1、已知,有n个人和m对好友关系
- 2、如果两个人是直接的或者间接的好友(好友的好友的好友。。。),那么他们属于一个集合,就是一个朋友圈中
- 3、写出程序,求这n个人中一共有多少个朋友圈
更详细的例子
https://blog.csdn.net/niushuai666/article/details/6662911
并查集核心函数
①初始化函数
使得每个父节点为自身,自成一派
②查找根节点与路径优化
由于根节点的父节点是自身,因此我们通过while来找到根节点。
路径优化是使得所有非根节点的直接父节点是根节点,即只有树的深度为2,先找到根节点,之后把所有根下面的节点的father直接改为根节点。
③两个集合求并
将集合1的根节点的父节点(原来是自身)连到集合2的根节点即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int father[maxn]; void init(){ for(int i = 0; i < maxn; i++)father[i] = i; //每个点自成一个集合 } int findPre(int x){ int a = x; while(x != father[x]) x = father[x]; //已经找到了其根节点x while(a != father[a]){ //路径优化 int temp = a; a = father[a]; father[temp] = x; //所有结点直接指向x,编程深度为2的树,提升效率 } return x; } void unionSet(int x, int y){ //两个集合并集 int fx = findPre(x); int fy = findPre(y); if(fx != fy) father[fx] = fy; } |
实例 PAT 1118 Birds in Forest (25 分)
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 () which is the number of pictures. Then lines follow, each describes a picture in the format:
...
where is the number of birds in this picture, and '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 .
After the pictures there is a positive number () which is the number of queries. Then 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:
1 2 3 4 5 6 7 8 9 |
4 3 10 1 2 2 3 4 4 1 5 7 8 3 9 6 4 2 10 5 3 7 |
Sample Output:
1 2 3 4 |
2 10 Yes No |
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include<iostream> #include<cstdio> using namespace std; const int maxn = 10001; int father[maxn], tree[maxn]; bool exist[maxn] = {false}; int findPre(int x){ int a = x; while(x != father[x]) x = father[x]; //已经找到了其根节点x while(a != father[a]){ //路径优化 int temp = a; a = father[a]; father[temp] = x; //所有结点直接指向x,编程深度为2的树,提升效率 } return x; } void unionSet(int x, int y){ //两个集合并集 int fx = findPre(x); int fy = findPre(y); if(fx != fy) father[fx] = fy; } int main(){ int num; cin>>num; for(int i = 0; i < maxn; i++)father[i] = i; //初始化每个点自成一个集合 while(num--){ int b, cur1, cur2; cin>>b>>cur1; exist[cur1] = true; b--; while(b--){ //输入每个集合 cin>>cur2; unionSet(cur1, cur2); exist[cur2] = true; } } for(int i = 1; i <= maxn; i++){ if(exist[i] == true){ int fa = findPre(i); tree[fa]++; } } int bnum = 0, tnum = 0; for(int i = 1; i <= maxn; i++){ if(tree[i] > 0){ tnum++; bnum += tree[i]; } } cout<<tnum<<" "<<bnum<<endl; int check, t1, t2; cin>>check; while(check--){ cin>>t1>>t2; printf("%s\n", findPre(t1)==findPre(t2)?"Yes":"No"); } return 0; } |