POJ2524并查集水题

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

题意:学校有n个同学,每个同学有且只有一个信仰,给出m对有同一信仰的同学,问存在多少种不同的信仰?输入0 0时结束程序。

思路:初始有n位同学,有n个集合,有n个信仰,对于给出的m对同学:若原来不知他们有同一信仰,则合并他们各自所在集合`信仰数减一。

#include <cstdio>

using namespace std;

#define max_n 50050

int n,m;
int par[max_n];

void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        par[i]=i;
    }
}

int find(int x)
{
    if(par[x]==x)
        return x;
    else
    {
        return par[x]=find(par[x]);
    }
}

bool same(int x,int y)
{
    return find(x)==find(y);
}

void unit(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    par[fx]=fy;
}

int main()
{
    int d=1;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
        init(n);
        int ans=n;
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(!same(x,y))
            {
                ans--;
                unit(x,y);
            }
        }
        printf("Case %d: %d\n",d,ans);
        d++;
    }
    return 0;
}

				
时间: 2024-07-29 10:35:26

POJ2524并查集水题的相关文章

poj2524(并查集水题)

题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input 10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0 Sample Output Case 1: 1Case 2: 7 解题思路:直接套并查集的板子就可以了,初始化n个集合默认他们都信任不一样的宗教,初始就用n个宗教,每次给你的两个同学那个号码将他

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

并查集水题 POJ2524

题意:一所学校有有n个学生,询问m对学生的宗教是否相同,求出这所学校最多有多少种宗教. 把宗教相同的学生连一条边,则未询问的学生默认他们没有边,最后连通块的个数就是宗教最多有多少个,并查集实现,把每个节点的最终父节点存到数组里,数组里不同元素的个数即为连通块的个数. 代码:

并查集水题 POJ 1611

题意:有n(n<=30000)个学生,每个学生属于一个团体(可以属于多个团体),共有m(<=500)个团体,如果一个学生是嫌疑人,则他所在的团体的所有人都是嫌疑人,初始时0号学生是嫌疑人.问总共有多少个嫌疑人. 很明显同一个团体的学生可以连一条边,即求0号点所在的连通块有多少个点,用并查集可以很方便的办到,如果两个点属于同一个连通块则把他们的代表元连接起来即可,始终把较小的那个节点作为父节点,所以最后p[0]的节点数就是答案. 代码:

hdu 4496 并查集 反向并查集 水题 D-City

觉得这道题以后可以和优先队列结合起来 嗯 就是说依次去掉前n条路求连通块数量 处理的时候  只要每次merge发现父亲不相等 然后进到里面合并的时候 num-- wa了好几次是因为最后输出的时候开了点的数量大小的数组而不是操作数量数组 orz D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5334    Accepted

HDU1863(Kruskal+并查集水题)

https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现请你编写程序,计算出全省畅通需要的最低成本. Input测试输入包含若干测试用例.每个测试用例的第1行给出评估的道路条数 N.村庄数目M ( < 100 ):随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的

【HDU1232】畅通工程(并查集基础题)

裸敲并查集,很水一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 #include <string> 1

poj 1182 食物链 并查集好题

挑战程序设计上有解答的并查集好题.把事件作为元素进行合并,举例:若输入1 2 3,意思就是把2,3归为同一类,至于归于哪一类并不需要去讨论,则把2属于A,3属于A这两件事件归为一类;2属于B,3属于B这两件事归为一类;2属于C,3属于C这两件事归为一类:若输入 2 2 3,由于A吃B,B吃C,C吃A,就把2属于A,3属于B这两件事情归为一类:以此类推.当检测到当前情况与之前正确的情况不符合,则错误的情况数加1. #include <iostream> #include <cstdio&g