POJ2524 Ubiquitous Religions(并查集)

题目:

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.

扫描二维码关注公众号,回复: 2809886 查看本文章

大意:

当今世界上有如此多的不同宗教,很难对它们进行追踪。你有兴趣了解你的大学里有多少不同宗教信仰的学生。

你知道你的大学里有N个学生(0<n<=50000)。你问每个学生他们的宗教信仰是不可行的。此外,许多学生不舒服地表达他们的信仰。避免这些问题的一种方法是问M(0<m=<=n(n-1)/ 2)对学生并询问他们是否相信同一宗教(例如他们可能知道他们是否都参加同一教会)。从这些数据中,你可能不知道每个人相信什么,但是你可以知道在校园里可能有多少种不同宗教的上限。你可以假设每个学生最多只订阅一种宗教,输出最大宗教数。

分析:

输出最大宗教数,就是求连通分支的个数;水的并查集,计数(通过判断根是否等于自身)即可。

附代码:

 1 #include<map>
 2 #include<cmath>
 3 #include<queue>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 int p[1000005],r[1000005];
11 int find(int x)
12 {
13     int r=x;
14     while (p[r]!=r)
15         r=p[r ];
16     int j,i=x;
17     while(i!=r)
18     {
19         j=p[i ];
20         p[i ]=r;
21         i=j;
22     }
23     return r;
24 }
25 int main()
26 {
27     int n,m,flag=0;
28     while(~scanf("%d%d",&n,&m))
29     {
30         flag++;
31         if(n==0&&m==0)
32             break;
33         for(int i=1; i<1000005; i++)
34             p[i]=i;
35         int sum=0;
36         for(int i=0; i<m; i++)
37         {
38             int f1,f2,p1,p2;
39             scanf("%d%d",&p1,&p2);
40             f1=find(p1);
41             f2=find(p2);
42             if(f1!=f2)
43                 p[f1]=f2;
44         }
45         for(int i=1; i<=n; i++)
46             if(p[i]==i)
47                 sum++;
48         printf("Case %d: %d\n",flag,sum);
49     }
50     return 0;
51 }

猜你喜欢

转载自www.cnblogs.com/She-Chuan/p/9492149.html