并查集的模板

并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。这一类问题近几年来反复出现在信息学的国际国内赛题中,其特点是看似并不复杂,但数据量极大,若用正常的数据结构来描述的话,往往在空间上过大,计算机无法承受;即使在空间上勉强通过,运行的时间复杂度也极高,根本就不可能在比赛规定的运行时间(1~3秒)内计算出试题需要的结果,只能用并查集来描述。
并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。
 

并查集可以快速的求解一个图的连通分量

快速查找加上路劲压缩

1  const int N = 10000;
2  int father[N];
3  int find_father(int x){
4    if(x == father[x]) return x;
5    int fa = fine_father(x);//寻找父亲
6    father[x] = fa;
7    return fa;
8 }
接下来是合并,每次输入数据之后进行一次合并
1 void Unite(int a, int b){
2     int x = find_father(a);
3     int y = find_father(b);
4     if(x != y) father[y] = x;
5 }

最后是初始化操作

1 void init(){
2     for(int i = 0; i < N; ++i){
3         p[i] = i;
4     }
5 }

将上述方法封装成类

 1 class Union{
 2 public:
 3     Union(){
 4         for(int i = 0; i < N; ++i) father[i] = i;
 5     }
 6 
 7     int find(const int &x){
 8         return x == father[x] ? x : father[x] = find(father[x]);//路径压缩
 9     }
10 
11     bool check(const int &x, const int &y){
12         return find(x) == find(y); 
13     }
14 
15     void merge(const int &x, const int &y){
16         if(!check(x, y)) father[father[x]] = father[y];
17     }
18 19 
20 private:
21     const static int N = 10000;
22     int father[N];
23 };

https://pintia.cn/problem-sets/994805342720868352/problems/994805354108403712例题链接

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 (≤) which is the number of pictures. Then N lines follow, each describes a picture in the format:

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 1.

After the pictures there is a positive number Q (≤) 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

AC代码:

 1 #include <iostream>
 2 using namespace std;
 3 class Union{
 4 public:
 5     Union(){
 6         for(int i = 0; i < N; ++i) father[i] = i;
 7     }
 8 
 9     int find(const int &x){
10         return x == father[x] ? x : father[x] = find(father[x]);//路径压缩
11     }
12 
13     bool check(const int &x, const int &y){
14         return find(x) == find(y); 
15     }
16 
17     void merge(const int &x, const int &y){
18         if(!check(x, y)) father[father[x]] = father[y];
19     }
20 
21 private:
22     const static int N = 10000;
23     int father[N];
24 };
25 
26 Union u;
27 int a[10000] = {0};
28 int main(){
29     int n; cin >> n;
30     int maxbird = 0;
31     for(int i = 1; i <= n; ++i){
32         int x; cin >> x; 
33         int first; cin >> first;
34         if(first > maxbird) maxbird = first;
35         while(--x){
36             int h; cin >> h;
37             if(h > maxbird) maxbird = h;
38             u.merge(first, h);   
39         }
40     }
41     int tree = 0;
42     for(int i = 1; i <= maxbird; ++i){
43         int fa = u.find(i);
44         a[fa]++;
45         if(a[fa] == 1) ++tree;
46     }
47     cout << tree << ' ' << maxbird << endl;
48     int m; cin >> m;
49     for(int i = 0; i < m; ++i){
50         int a, b; cin >> a >> b;
51         u.find(a) == u.find(b) ? cout << "Yes\n" : cout << "No\n";
52     }
53     return 0;
54 }

这个代码每次测试点3,4时间都不一样。。。。。。。。。。。。

猜你喜欢

转载自www.cnblogs.com/MasterYan576356467/p/11211898.html
今日推荐