poj 2524 Ubiquitous Religions
来源:互联网 发布:依爱消防火灾报警编程 编辑:程序博客网 时间:2024/04/29 02:57
Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 32283 Accepted: 15644
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
【分析】
一开始假设大家都各自信仰一个宗教,那么总的数目ans就是学生数目,每当发现有一对学生信仰同一个宗教,那么ans–-;
【代码】
//poj 2524 Ubiquitous Religions#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;int fa[50005];int n,m,ans,t;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}inline int find(int x){ if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x];}int main(){ int i,j,x,y; while(scanf("%d%d",&n,&m) && n && m) { t++; ans=n; fo(i,1,n) fa[i]=i; fo(i,1,m) { x=read();y=read(); if(find(x)!=find(y)) fa[find(x)]=find(y),ans--; } printf("Case %d: %d\n",t,ans); } return 0;}
- POJ 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ-2524 Ubiquitous Religions
- Poj 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 - Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- poj 2524 Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- POJ-2524-Ubiquitous Religions
- POJ 2524 Ubiquitous Religions
- [php] [教程] 之ThinkPHP视频教程(PHP系列2015版)
- 4.3.2 线索二叉树
- Barricade
- 据说iPhone 7有谜之噪音 它究竟是什么东西
- CSS常用选择器汇总
- poj 2524 Ubiquitous Religions
- hdu 2012A计划 搜索
- python中list中方法
- Gym 100703A Tea-drinking 最小生成树
- 教育平台在线,教育平台源码 类似eduline教育平台系统源码下载
- ubuntu常见问题
- EditText三个编辑框输入,动态改变登陆按钮的颜色变化
- 关于树的经典算法题
- 在线教育专业建站工具 EduWind ,源码下载,源码分享网整理