并查集模板(题目应用 POJ2524)

Ubiquitous Religions
Time Limit: 5000MS

Memory Limit: 65536K
Total Submissions: 40520

Accepted: 19302
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
Hint
Huge input, scanf is recommended.
并查集有个基本的模板,其余的并查集的延申都会是从这个模板中延申来的。
并查集的查询有两种方式,一种是循环,一种是递归。各有各的好处吧,想用什么就看自己了,前面我写了循环的,后面我会补充一个递归实现的。

//并查集,模板题
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#define MAXN 50005
using namespace std;

//并查集的关系数组,主要是用来表示各个节点之间的关系
int father[MAXN];
int n,m;
int sum;//用来记录团体数目

//寻找根节点
int mfind(int x){
    int r = x;
    while(r!=father[r]){
        r = father[r];
    }
    int i = x,j;
    //下面是压缩路径
    while(i!=r){
        j= father[i];
        father[i] = r;
        i = j;
    }
    return r;
}
//将两个节点放到一个集合里
void connect(int x,int y){
    int nx = mfind(x);
    int ny = mfind(y);
    if(nx!=ny){
        father[nx] = ny;
        sum--;//两个团体和为一个团体,团体数目减一
    }
}
//每次使用之前必须进行初始化
void init(){
    for(int i=0;i<MAXN;i++){
        father[i] = i;
    }
}

int main()
{
    int j = 1;
    while(~scanf("%d%d",&n,&m)&&m+n){
        init();
        sum=n;
        for(int i=0;i<m;i++){
            int people_1,people_2;
            scanf("%d%d",&people_1,&people_2);
            connect(people_1,people_2);
        }
        printf("Case %d: %d\n",j++,sum);
    }
    return 0;

}

运行结果:
880K
282MS

//并查集,模板题
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#define MAXN 50005
using namespace std;

//并查集的关系数组,主要是用来表示各个节点之间的关系
int father[MAXN];
int n,m;
int sum;//用来记录团体数目

//寻找根节点
int mfind(int x){
    if(father[x]!=x){
        //查询和压缩路径
        father[x] = mfind(father[x]);
    }
    return father[x];
}
//将两个节点放到一个集合里
void connect(int x,int y){
    int nx = mfind(x);
    int ny = mfind(y);
    if(nx!=ny){
        father[nx] = ny;
        sum--;//两个团体和为一个团体,团体数目减一
    }
}
//每次使用之前必须进行初始化
void init(){
    for(int i=0;i<MAXN;i++){
        father[i] = i;
    }
}

int main()
{
    int j = 1;
    while(~scanf("%d%d",&n,&m)&&m+n){
        init();
        sum=n;
        for(int i=0;i<m;i++){
            int people_1,people_2;
            scanf("%d%d",&people_1,&people_2);
            connect(people_1,people_2);
        }
        printf("Case %d: %d\n",j++,sum);
    }
    return 0;

}

运行结果:
880K
313MS
相对来说循环会快一些,但循环的时候,有了权值之后,权值的更新范围会更广,个人推荐递归多一点,但是最好是都会。

猜你喜欢

转载自blog.csdn.net/weixin_40488730/article/details/81584341