一直以来对于并查集的定义都感觉很模糊,看见网上的资料都是一知半解,今天认真地研究了一下,然后经孟海同志点拨了一下。。。才发现自己真的很愚蠢——居然连并查集都没有搞懂。
维基百科上面的并查集:
在我刚开始的时候一直没有明白的就是为什么要用并查集,对于其中的合并操作十分的不理解,其实有一点我一直都没有注意,并查集本质上只是适用于某些特殊的搜索情景,比如给你N条边的两端点坐标,然后找出这些边形成了多少个图,亦或者通俗点,告知N个人相互之间的血缘关系,然后求有这N个人当中总共来自多少个家族。。。知道了这些过后,就不难明白为什么并查集只关心每个点的祖宗结点了(不是父结点!,其实祖宗结点也是父结点,不过这里区分开来说,主要是与单纯的上一层父结点区别开来),它本质上只是将集合的元素进行大概分类,仅此而已。
—
来看一道理论题目:
对于以下等价类,采用「加权合并规则」(也称「重量权衡合并规则」),进行并查运算,给出最后父节点索引序列。
4-0 6-2 8-4 9-4 3-5 9-5 5-2 1-2 7-1
注意:
当合并大小相同的两棵树的时候,将第二棵树的根指向第一棵树的根;根节点的索引是它本身;数字之间只用一个空格隔开。
(题目来自Coursera上的北大数据结构与算法课程)
按照之前的思路,这道题每次结点合并过程中祖宗结点的索引如下(第N个数就是N的祖宗结点索引,N从0开始):
0 1 2 3 4 5 6 7 8 9 //初始化,每个结点的祖宗结点都是本身
4 1 2 3 4 5 6 7 8 9//第一次make union,0和4合并,0的祖宗结点变成4的祖宗结点,即4
4 1 6 3 4 5 6 7 8 9
4 1 6 3 4 5 6 7 4 9
4 1 6 3 4 5 6 7 4 4
4 1 6 3 4 3 6 7 4 4
4 1 6 4 4 3 6 7 4 4
4 1 6 4 4 3 4 7 4 4
4 4 6 4 4 3 4 7 4 4
4 4 6 4 4 3 4 4 4 4
—
然后再来两道并查集入门题目:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 29142 | Accepted: 14037 |
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
Output
Sample Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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
1 2 |
Case 1: 1 Case 2: 7 |
Hint
Source
题意
有n个人,编号分别为1, 2, 3 … n,然后在m个输入里面,每次输入x和y,即x和y是属于同一个religion的,最后这n个人一共信仰了多少个religions。
代码
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
/* * File Name: poj2524.cpp * Author: razrLeLe * Mail: razrlele@gmail.com */ #include <vector> #include <set> #include <deque> #include <queue> #include <algorithm> #include <functional> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> #include <string.h> #include <map> #include <cctype> #include <list> #include <stack> #define forn(i, m, n) for (int i = int(m); i < int(n); i++) typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 50007; int ancient[maxn]; void make_set(int n) { forn(i, 1, n + 1) { ancient[i] = i; } return ; } int find(int a) { while (a != ancient[a]) a = ancient[a]; return a; } void make_union(int a, int b) { int x = find(a); int y = find(b); if (x == y) return; else ancient[x] = y; } int main(int argc, char** argv) { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif int n, m; int count = 0; while (scanf("%d %d", &n, &m) ) { if( !n ) break ; int a, b; make_set(n); while (m--) { scanf("%d %d", &a, &b); make_union(a, b); } int ans = 0; forn(i, 1, n + 1) if (i == ancient[i]) ++ans ; printf("Case %d: %d\n", ++count, ans); } return 0; } |
—
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 28635 | Accepted: 13949 |
Description
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
Sample Input
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 |
4 1 1 |
Source
题意
n个学生的编号分别为0, 1, 2, … n-1然后在m个输入里面,开始先输入一个k,然后在接下来输入k个学生的编号,表示这些学生的是属于同一个group,每个学生可以同时属于多个group,然后问有多少个学生与编号为0的学生有关系(直接或者间接地在同一个group,比如0和1在同一个group,1又和2在同一个group,则0和2有关系)
代码
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
/* * File Name: poj1611.cpp * Author: razrLeLe * Mail: razrlele@gmail.com */ #include <vector> #include <set> #include <deque> #include <queue> #include <algorithm> #include <functional> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> #include <string.h> #include <map> #include <cctype> #include <list> #include <stack> #define forn(i, m, n) for( int i = int(m); i < int(n); i++ ) typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 30007; int ancient[maxn]; void make_set( int n ){ forn( i, 0, n ) ancient[i] = i; } int find( int a ){ while( a != ancient[a] ) a = ancient[a]; return a; } void make_union( int a, int b ){ int x = find( a ); int y = find( b ); if( x == y ) return ; else ancient[x] = y; return ; } int main(int argc, char ** argv) { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif int n, m; while( scanf( "%d%d", &n ,&m ) ){ if( !n ) break; make_set( n ); int x, y, k; while( m-- ){ scanf("%d %d", &k, &x); --k; while( k-- ){ scanf("%d", &y); make_union( x, y ); } } int ans = 1; int zero = find(0); forn( i, 1, n ){ if( find(i) == zero ) ans++; } printf("%d\n", ans); } return 0; } |