poj2524 Ubiquitous Religions(并查集求组数)
来源:互联网 发布:热血传奇手游脚本源码 编辑:程序博客网 时间:2024/06/07 01:01
Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 21883 Accepted: 10739
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.
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
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int pre[50004],num[50004];int find(int a){ while(pre[a]!=a) a=pre[a]; return a;}void uni(int a,int b){ int x=find(a),y=find(b); if(x==y) return; if(num[x]<num[y]) { int r=x; x=y; y=r; } pre[y]=x; num[x]+=num[y];}int main(){int cas=1,n,m,i,t,a,b;while(scanf("%d%d",&n,&m),n){for(i=1;i<=n;i++){pre[i]=i;num[i]=1;}for(t=0;t<m;t++){scanf("%d%d",&a,&b);uni(a,b);}for(i=1;i<=n;i++)pre[i]=find(i);sort(pre+1,pre+n+1);//排序后祖先相同的都凑在一起了int ans=1;for(i=2;i<=n;i++){if(pre[i]!=pre[i-1])ans++;//数堆数即可}printf("Case %d: %d\n",cas++,ans);} return 0;}
0 0
- poj2524 Ubiquitous Religions(并查集求组数)
- POJ2524--Ubiquitous Religions(并查集)
- POJ2524——Ubiquitous Religions(并查集)
- POJ2524 - Ubiquitous Religions (并查集的基础应用)
- poj2524——Ubiquitous Religions(并查集)
- hdu1232 畅通工程 && poj2524 Ubiquitous Religions(并查集裸)
- POJ2524 Ubiquitous Religions 并查集
- poj2524 Ubiquitous Religions 并查集入门
- POJ2524 Ubiquitous Religions 并查集
- 【并查集】:poj2524,Ubiquitous Religions
- 并查集-poj2524-Ubiquitous Religions
- POJ2524 Ubiquitous Religions(并查集)
- POJ2524 Ubiquitous Religions 并查集
- POJ2524 Ubiquitous Religions(简单并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524 Ubiquitous Religions (并查集)
- poj2524 Ubiquitous Religions
- vs2008 编译前自动执行脚本
- 学习杂感
- 操作码8:异常
- MySQL中timestamp(时间戳)自动更新详解
- EditText自动弹出键盘问题处理
- poj2524 Ubiquitous Religions(并查集求组数)
- 安装fedora9和安装VM tools实现文件共享
- 操作码9:finally子句
- 操作码10:方法的调用与返回
- 手机卡上写入SqLite数据
- poj 2299 Ultra-QuickSort(树状数组+离散化的题目)据说是简单题,不过还是觉得好难。。。
- 编译hadoop eclipse 2.x插件
- 操作码11:线程同步
- 枚举的应用——求天数