并查集--学习详解

文章作者: yx_th000 文章来源: Cherish_yimi (http://www.cnblogs.com/cherish_yimi/) 转载请注明,谢谢合作。
    
    
昨天和今天学习了并查集和 trie 树,并练习了三道入门题目,理解更为深刻,觉得有必要总结一下,这其中的内容定义之类的是取自网络,操作的说明解释及程序的注释部分为个人理解。

   
并查集学习:

l         并查集:(union-find sets)

一种简单的用途广泛的集合. 并查集是若干个不相交集合,能够实现较快的合并和判断元素所在集合的操作,应用很多,如其求无向图的连通分量个数等。最完美的应用当属:实现Kruskar算法求最小生成树。

l         并查集的精髓(即它的三种操作,结合实现代码模板进行理解):

1、Make_Set(x) 把每一个元素初始化为一个集合

初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变)。

2、Find_Set(x) 查找一个元素所在的集合

查找一个元素所在的集合,其精髓是找到这个元素所在集合的祖先这个才是并查集判断和合并的最终依据。
判断两个元素是否属于同一集合,只要看他们所在集合的祖先是否相同即可。
合并两个集合,也是使一个集合的祖先成为另一个集合的祖先,具体见示意图

3、Union(x,y) 合并x,y所在的两个集合

合并两个不相交集合操作很简单:
利用Find_Set找到其中两个集合的祖先,将一个集合的祖先指向另一个集合的祖先。如图

l         并查集的优化

1、Find_Set(x)时 路径压缩
寻找祖先时我们一般采用递归查找,但是当元素很多亦或是整棵树变为一条链时,每次Find_Set(x)都是O(n)的复杂度,有没有办法减小这个复杂度呢?
答案是肯定的,这就是路径压缩,即当我们经过"递推"找到祖先节点后,"回溯"的时候顺便将它的子孙节点都直接指向祖先,这样以后再次Find_Set(x)时复杂度就变成O(1)了,如下图所示;可见,路径压缩方便了以后的查找。

2、Union(x,y)时 按秩合并
即合并的时候将元素少的集合合并到元素多的集合中,这样合并之后树的高度会相对较小。

l         主要代码实现

Code
 1int father[MAX];   /**//* father[x]表示x的父节点*/
 2int rank[MAX];     /**//* rank[x]表示x的秩*/
 3
 4
 5 /**//* 初始化集合*/
 6void Make_Set(int x)
 7 {
 8     father[x] = x; //根据实际情况指定的父节点可变化
 9    rank[x] = 0;   //根据实际情况初始化秩也有所变化
10}
11
12
13 /**//* 查找x元素所在的集合,回溯时压缩路径*/
14int Find_Set(int x)
15 {
16     if (x != father[x])
17     {
18         father[x] = Find_Set(father[x]); //这个回溯时的压缩路径是精华
19    }
20     return father[x];
21 }
22
23
24 /**//* 
25    按秩合并x,y所在的集合
26    下面的那个if else结构不是绝对的,具体根据情况变化
27    但是,宗旨是不变的即,按秩合并,实时更新秩。
28 */
29void Union(int x, int y)
30 {
31     x = Find_Set(x);
32     y = Find_Set(y);
33     if (x == y) return;
34     if (rank[x] > rank[y]) 
35     {
36         father[y] = x;
37     }
38     else
39     {
40         if (rank[x] == rank[y])
41         {
42             rank[y]++;
43         }
44         father[x] = y;
45     }
46 }
47

 

注:学习并查集时非常感谢Slyar提供的资料,这里注明链接:http://www.slyar.com/blog/
另外,我认为写并查集时涉及到的路径压缩,最好用递归,一方面代码的可读性非常好,另一方面,可以更直观的理解路径压缩时在回溯时完成的巧妙。

 

 

PKU POJ 1161解题报告(并查集)

文章作者:yx_th000 文章来源:Cherish_yimi (http://www.cnblogs.com/cherish_yimi/) 转载请注明,谢谢合作。

并查集学习--并查集详解

The Suspects

Time Limit: 1000MS

 

Memory Limit: 20000K

Total Submissions: 5572

 

Accepted: 2660

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

100 4

2 1 2

5 10 13 11 12 14

2 0 1

2 99 2

200 2

1 5

5 1 2 3 4 5

1 0

0 0

Sample Output

4

1

1

我的思路:

典型的并查集,最初各自为集,然后每个group进行合并,等到所有的group合并完,题目也就解决了,因为在合并的时候,如果哪两个group中有重合的元素,则那个后来的group会由于按秩合并的原则自动合并到

先有的集合当中,奥妙便在其中。下面是代码:

Code
 1 #include
 2using namespace std;
 3
 4int n, m, i, j;
 5int father[30005], num[30005];
 6
 7void makeSet(int n)
 8 {
 9     for(i = 0; i < n; i++)
10     {
11         father[i] = i; //使用本身做根
12        num[i] = 1;
13     }
14 }
15int findSet(int x)
16 {
17     if(father[x] != x) //合并后的树的根是不变的
18    {    
19         father[x] = findSet(father[x]);
20     }
21     return father[x]; 
22 }
23
24void Union(int a, int b)
25 {
26     int x = findSet(a);
27     int y = findSet(b);
28     if(x == y)
29     {
30         return;
31     }
32     if(num[x] <= num[y])
33     {
34         father[x] = y;
35         num[y] += num[x];
36     }
37     else 
38     {
39         father[y] = x;
40         num[x] += num[y];
41     }
42 }
43
44int main()
45 {
46     while(scanf("%d %d", &n, &m)!=EOF && n != 0)
47     {
48         makeSet(n);
49         for(i = 0; i < m; i++)
50         {
51             int count, first, b;
52             scanf("%d %d",&count, &first);
53             for(j = 1; j < count; j++)
54             {
55                 scanf("%d",&b);
56                 Union(first,b);
57             }
58         }
59         printf("%d\n",num[findSet(0)]);
60     }
61     return 0;
62 }
63

另外,上面并查集的根我是采用数字本身的,然后路径压缩寻找父亲节点是采用递归的,下面贴出,采用-1做根和使用非递归实现的部分代码:

Code
 1void makeSet(int n)
 2 {
 3     for(i = 0; i < n; i++)
 4     {
 5         father[i] = -1;
 6         num[i] = 1;
 7     }
 8 }
 9//非递归实现
10int findSet(int x)
11 {
12     while(father[x] != -1)   //根为-1
13    {
14         x = father[x];
15     }
16     return x;
17 }
18

 

PKU POJ 2524 解题报告(并查集)

文章作者:yx_th000 文章来源:Cherish_yimi (http://www.cnblogs.com/cherish_yimi/) 转载请注明,谢谢合作。

并查集学习--并查集详解
 

Ubiquitous Religions

Time Limit: 5000MS

 

Memory Limit: 65536K

Total Submissions: 9637

 

Accepted: 4463

Description

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.


Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.


Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.


Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1

Case 2: 7

我的思路:

有了前面学习的基础和1161的练习,这道题完全就可以水过了,设定一个计数器,每次合并时减1便可值得一提的是:树的秩是好东西啊,既可以记录每个集合的节点个数,又能保证按秩合并成相对高度小的树。

代码如下:

Code
 1 #include
 2using namespace std;
 3
 4int n, m, maxNum, i;
 5int father[50005], num[50005];
 6
 7void makeSet(int n)
 8 {
 9     for(int j = 1; j <= n; j++)
10     {
11         father[j] = j;
12         num[j] = 1;
13     }
14 }
15int findSet(int x)
16 {
17     if(father[x] != x) 
18     {    
19         father[x] = findSet(father[x]);
20     }
21     return father[x]; 
22 }
23
24void Union(int a, int b)
25 {
26     int x = findSet(a);
27     int y = findSet(b);
28     if(x == y)
29     {
30         return;
31     }
32     if(num[x] <= num[y])
33     {
34         father[x] = y;
35         num[y] += num[x];
36         maxNum--;
37     }
38     else 
39     {
40         father[y] = x;
41         num[x] += num[y];
42         maxNum--;
43     }
44 }
45
46int main()
47 {
48     int Case = 1;
49     while(scanf("%d %d", &n, &m)!=EOF && n!=0)
50     {
51         maxNum = n;
52         makeSet(n);
53         int a, b;
54         for(i = 0; i < m; i++)
55         {
56             scanf("%d %d",&a, &b);
57             Union(a,b);
58         }
59         printf("Case %d: %d\n",Case++,maxNum);
60     }
61     return 0;
62 }
63

POJ 1611 The Suspects          最基础的并查集

 

POJ 2524 Ubiquitous Religions 最基本的并查集

POJ 1182 食物链       并查集的拓展

注意: 只有一组数据;

要充分利用题意所给条件:有三类动物A,B,C,这三类动物的食物链

构成了有趣的环形。A吃B, B吃C,C吃A。也就是说:只有三个group

POJ 2492 A Bug's Life 并查集的拓展

法一:深度优先遍历

每次遍历记录下该点是男还是女,只有:男-〉女,女-〉男满足,否则,找到同性恋,结束程序。

法二:二分图匹配

法三:并查集的拓展:和1182很像,只不过这里就有两组,而1182是三组,1611无限制

POJ 1861 Network == zju_1542    并查集+自定义排序+贪心求"最小生成树"

答案不唯一,不过在ZOJ上用QSORT()和SORT()都能过,在POJ上只有SORT()才能过...

POJ 1703 Find them, Catch them 并查集的拓展

这个和POJ 2492 A Bug's Life很像,就是把代码稍微修改了一下就AC了!

注意:And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. 就是说只有两个组。

POJ 2236 Wireless Network        并查集的应用

需要注意的地方:1、并查集;2、N的范围,可以等于1001;3、从N+1行开始,第一个输入的可以是字符串。

POJ 1988 Cube Stacking            并查集很好的应用

1、与 银河英雄传说==NOI2002 Galaxy一样;2、增加了一个数组behind[x],记录战舰x在列中的相对位置;3、详细解题报告见银河英雄传说。

 

JOJ 1905 Freckles   == POJ 2560 最小生成树

 

法一:Prim算法;法二:并查集实现Kruskar算法求最小生成树

 

你可能感兴趣的:(algorithm)