Wireless Network (并查集)

来源:互联网 发布:马鞍山网络大学招聘 编辑:程序博客网 时间:2024/06/05 15:20

Ubiquitous Religions


Time limit    10000 ms          Memory limit    65536 kB




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 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Sample Output
Case 1: 1Case 2: 7
Hint

Huge input, scanf is recommended.


题解:给你n,m,然后给你m行a,b,表示a,b有共同的宗教信仰,求最大的宗教数量,(最大为n)……


#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>#include<stdlib.h>#include<time.h>#include<string>#include<math.h>#include<map>#include<queue>#include<stack>#define INF 0x3f3f3f3f#define ll __int64#define For(i,a,b) for(int i=a;i<b;i++)#define sf(a)  scanf("%d",&a)#define sfs(a)  scanf("%s",a)#define sff(a,b)  scanf("%d%d",&a,&b)#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)#define pf(a) printf("%d\n",a)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;#define maxn 105#define maxv 10005int pre[50005],x[50005];int find(int x)                   // 查找根节点{    int s=x;    while(s!=pre[s])              // 查找根节点    {        s=pre[s];    }    int i=x,j;    while(pre[i]!=s)               //路径压缩,节省查询时间    {        j=pre[i];        pre[i]=s;        i=j;    }    return s;}void join(int x,int y)            // 判断x,y是否拥有共同的宗教信仰,若有就不管了,若没有,让两人的信仰相同{    int xx=find(x);int yy=find(y);    if(xx!=yy)        pre[xx]=yy;}int main(){    int n,M,flag=0;    while(~sff(n,M)&&(n||M))    {        flag++;        for(int i=1;i<=n;i++)     // 初始化各点的根节点为自己本身            pre[i]=i;        int a,b;        For(i,0,M)        {            sff(a,b);            join(a,b);           // 处理数据        }        mem(x,0);        for(int i=1;i<=n;i++)        {            x[find(i)]=1;        // 标记根节点        }        int s=0;        for(int i=1;i<=n;i++)     // 记录根节点的数目,即最大宗教的数量            if(x[i])            {                s++;            }        printf("Case %d: ",flag);        printf("%d\n",s);    }}


0 0
原创粉丝点击