您的位置:首页 > 产品设计 > UI/UE

POJ 2524 Ubiquitous Religions(简单的并查集)

2014-07-27 10:29 483 查看
Ubiquitous Religions

点击打开题目链接

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 23669Accepted: 11654
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.

题目就是说把信仰相同的放到同一个集合中,判断集合的个数并输出;

并查集(树实现)的基本操作有三个:

先定义一个树节点:

struct uftree
{
    int cnt;//记录集合中元素的个数
    int rank;//记录树的高度
    int parent;//记录父节点
} set[MAX];
1:对于并查集树的初始化:

void make_set(int n)
{
    int i;
    for(i=0; i<=n; i++)
    {
        set[i].cnt=1;//开始时至少有一个元素
        set[i].rank=0;//高度为0;
        set[i].parent=i;//父节点是自己
    }
}
2:查找一个元素所属的集合



int set_find(int x)//查找元素x所属的集合
{
    if(set[x].parent==x)//如果父节点时本身则返回x
        return x;
    else
        return set[x].parent=set_find(set[x].parent);//(边查找边路径压缩)返回x所在树的根节点并把x的父节点指向根节点
//return set_find(set[x].parent);//未经路径压缩的算法
 }
3:合并两个集合



合并过程大概如上图:

void set_join(int x,int y)
{
    int a=set_find(x);//找到两元素所在树的根节点
    int b=set_find(y);
    if(set[a].rank>set[b].rank)
    {
        set[b].parent=a;//合并(即将b的父节点指向a)
        if(a!=b)
        set[a].cnt+=set[b].cnt;//计算当前树高一下有多少个元素
    }
    else
    {
        set[a].parent=b;
        if(a!=b)
        set[b].cnt+=set[a].cnt;//计算当前树高一下有多少个元素
        if(set[a].rank==set[b].rank)//树高相同时让父节点的树高值加一
        {
            set[b].rank++;
        }
    }
}


此题代码:

#include <iostream>
#include<string.h>
#include<stdio.h>
#define MAX 50010
using namespace std;
struct uftree
{
    int cnt;
    int rank;
    int parent;
} set[MAX];
void make_set(int n)
{
    int i;
    for(i=0; i<=n; i++)
    {
        set[i].cnt=1;
        set[i].rank=0;
        set[i].parent=i;
    }
}
int set_find(int x)
{
    if(set[x].parent==x)
        return x;
    else
        return set[x].parent=set_find(set[x].parent);
}
void set_join(int a,int b)//此题的合并比较简单不必再那样
{
    if(set_find(a)!=set_find(b))
    {
        set[set_find(a)].parent=set_find(b);
    }
}
int query(int n)
{
    int i,ans=0;
    for(i=0; i<n; i++)
    {
        if(set[i].parent==i)
            ans++;
    }
    return ans;
}
int main()
{
    int t=0,n,m,i,a,b,ans;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        //memset(set,-1,sizeof(set));
        make_set(n);
        for(i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            set_join(a,b);
        }
        ans=query(n);
        printf("Case %d: %d\n",++t,ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: