现在的位置: 首页 > 综合 > 正文

并查集 (Union-Find Sets)及其应用

2013年10月03日 ⁄ 综合 ⁄ 共 5542字 ⁄ 字号 评论关闭

并查集 (Union-Find Sets)

并查集:(union-find sets)是一种简单的用途广泛的集合. 并查集是若干个不相交集合,能够实现较快的合并和判断元素所在集合的操作,应用很多。一般采取树形结构来存储并查集,并利用一个rank数组来存储集合的深度下界,在查找操作时进行路径压缩使后续的查找操作加速。这样优化实现的并查集,空间复杂度为O(N),建立一个集合的时间复杂度为O(1)N次合并M查找的时间复杂度为O(M Alpha(N)),这里AlphaAckerman函数的某个反函数,在很大的范围内(人类目前观测到的宇宙范围估算有1080次方个原子,这小于前面所说的范围)这个函数的值可以看成是不大于4的,所以并查集的操作可以看作是线性的。它支持以下三中种操作:
  -Union (Root1, Root2) //并操作;把子集合Root2并入集合Root1.要求:Root1 Root2互不相交,否则不执行操作
.
  -Find (x) //搜索操作;搜索单元素x所在的集合,并返回该集合的名字
.
  -UFSets (s) //构造函数。将并查集中s个元素初始化为s个只有一个单元素的子集合
.
  -对于并查集来说,每个集合用一棵树表示。

  -集合中每个元素的元素名分别存放在树的结点中,此外,树的每一个结点还有一个指向其双亲结点的指针。
  -设 S1= {0, 6, 7, 8 }S2= { 1, 4, 9 }S3= { 2, 3, 5 }


-为简化讨论,忽略实际的集合名,仅用表示集合的树的根来标识集合。
  -为此,采用树的双亲表示作为集合存储表示。集合元素的编号从0 n-1。其中 n 是最大元素个数。在双亲表示中,第 i 个数组元素代表包含集合元素 i 的树结点。根结点的双亲为-1,表示集合中的元素个数。为了区别双亲指针信息( ≥ 0 ),集合元素个数信息用负数表示。
   

下标

parent

集合S1, S2S3的双亲表示:

                             S1 S2的可能的表示方法

const int DefaultSize = 10;
  class UFSets { //并查集的类定义

  private:
   
int *parent;
   
int size;
  
public:
   
UFSets ( int s = DefaultSize );
   
~UFSets ( ) { delete [ ] parent; }
   UFSets & operator = ( UFSets const & Value );//集合赋值

   void Union ( int Root1, int Root2 );
   
int Find ( int x );
   
void UnionByHeight ( int Root1, int Root2 ); };
   UFSets::UFSets ( int s ) { //构造函数

   size = s;
   
parent = new int [size+1];
   
for ( int i = 0; i <= size; i++ ) parent[i] = -1;
  }

  unsigned int UFSets::Find ( int x ) { //搜索操作
   if ( parent[x] <= 0 ) return x;
   
else return Find ( parent[x] );
  }

  void UFSets::Union ( int Root1, int Root2 ) { //
   parent[Root2] = Root1; //Root2指向Root1
  }

FindUnion操作性能不好。假设最初 n 个元素构成 n 棵树组成的森林,parent[i] = -1。做处理Union(0, 1), Union(1, 2), …, Union(n-2, n-1)后,将产生如图所示的退化的树。

                            

执行一次Union操作所需时间是O(1)n-1Union操作所需时间是O(n)。若再执行Find(0), Find(1), …, Find(n-1), 若被
搜索的元素为i,完成Find(i)操作需要时间为O(i),完成 n 次搜索需要的总时间将达到
              

Union操作的加权规则

  为避免产生退化的树,改进方法是先判断两集合中元素的个数,如果以 i 为根的树中的结点个数少于以 j 为根的树中的结点个数,即parent[i] > parent[j],则让 j 成为 i 的双亲,否则,让i成为j的双亲。此即Union的加权规则。

              parent[0](== -4) < parent[4] (== -3)

 

  void UFSets::WeightedUnion(int Root1, int Root2) {
   //Union的加权规则改进的算法

   int temp = parent[Root1] + parent[Root2];
   
if ( parent[Root2] < parent[Root1] ) {
    parent[Root1] = Root2; //Root2中结点数多

    parent[Root2] = temp;  //Root1指向Root2
   }
   
else {
    parent[Root2] = Root1; //Root1中结点数多

    parent[Root1] = temp;  //Root2指向Root1
   }
  }

                            使用加权规则得到的树

 

下面是几到用并查集可以方便解决的问题:

 

题目: 亲戚(Relations)

或许你并不知道,你的某个朋友是你的亲戚。他可能是你的曾祖父的外公的女婿的外甥的表姐的孙子。如果能得到完整的家谱,判断两个人是否亲戚应该是可行的,但如果两个人的最近公共祖先与他们相隔好几代,使得家谱十分庞大,那么检验亲戚关系实非人力所能及.在这种情况下,最好的帮手就是计算机。

为了将问题简化,你将得到一些亲戚关系的信息,如同MarryTom是亲戚,TomB en是亲戚,等等。从这些信息中,你可以推出MarryBen是亲戚。请写一个程序,对于我们的关心的亲戚关系的提问,以最快的速度给出答案。

参考输入输出格式 输入由两部分组成。

第一部分以NM开始。N为问题涉及的人的个数(1 N 20000)。这些人的编号为1,2,3,,N。下面有M(1 M 1000000),每行有两个数ai, bi,表示已知aibi是亲戚.

第二部分以Q开始。以下Q行有Q个询问(1 Q 1 000 000),每行为ci, di,表示询问cidi是否为亲戚。

对于每个询问ci, di,若cidi为亲戚,则输出Yes,否则输出No

样例输入与输出

输入relation.in

10 7

2 4

5 7

1 3

8 9

1 2

5 6

2 3

3

3 4

7 10

8 9

输出relation.out

Yes

No

Yes

如果这道题目不用并查集,而只用链表或数组来存储集合,那么效率很低,肯定超时。

例程:

 

#include<iostream>

using namespace std;

int N,M,Q;

int pre[20000],rank[20000];

void makeset(int x)

 {

     pre[x]=-1;

     rank[x]=0;

 }

int find(int x)

 {

     int r=x;

     while(pre[r]!=-1)

      r=pre[r];

     while(x!=r)

      {

          int q=pre[x];

          pre[x]=r;

          x=q;

      }

    return r;      

 }    

void unionone(int a,int b)

 {

     int t1=find(a);

     int t2=find(b);

     if(rank[t1]>rank[t2])

       pre[t2]=t1;

    else

       pre[t1]=t2;

    if(rank[t1]==rank[t2])

      rank[t2]++;     

 }       

int main()

 

{

   int i,a,b,c,d;

    while(cin>>N>>M)

     {

         for(i=1;i<=N;i++)

          makeset(i);

        for(i=1;i<=M;i++)

          {

              cin>>a>>b;

              if(find(a)!=find(b))

               unionone(a,b);

          }

        cin>>Q; 

        for(i=1;i<=Q;i++)

         {

             cin>>c>>d;

             if(find(c)==find(d))

              cout<<"YES"<<endl;

             else

              cout<<"NO"<<endl; 

         }           

     }   

    return 0;

}

 

ZJU1789The Suspects

【问题描述】

 

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

【算法分析】

 

这道题的意思很简单,n个人编号,0n-1,n个人分成m个集合(1个人可以参加不同的集合),求的就是最后所有和0号有关系的集合的

人数.

如果这道题目不用并查集,而只用链表或数组来存储集合,那么效率很低,肯定超时.我们在题目给出的每个集合的人员编号时,进行并查

操作,不过在进行合并操作时,合并的是两个集合的元素个数.最后0号元素所在的集合数目就是所求.

例程:

#include<iostream>

#include<cstdio>

using namespace std;

const int size=30000;

int pre[size],num[size];

int n,m,k;

void makeset(int x)

 {

     pre[x]=-1;

     num[x]=1;

 }

int find(int x)//非递归压缩路径

 {

     int r=x;

抱歉!评论已关闭.