POJ 2524 Ubiquitous Religions 并查集
来源:互联网 发布:计算机二级vb题库软件 编辑:程序博客网 时间:2024/05/16 16:10
Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 23215 Accepted: 11435
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<iostream>#include<cstring>using namespace std;int v[500005];int f[500005];int find(int x){return f[x] == x ? x : find(f[x]);}int main(){int n,m,k=0;while(cin>>n>>m){if(n==0&&m==0) break;int i,j;memset(f,0,sizeof(f));for(i=0;i<n;i++){f[i]=i;v[i]=0;}int x,y;for(i=0;i<m;i++){cin>>x>>y;f[find(x)]=find(y);}int sum=0;for(i=0;i<n;i++){if(!v[i]&&f[i]==i){v[i]=1;sum++;}}cout<<"Case "<<++k<<": "<<sum<<endl;}return 0;}
0 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 并查集
- Java Web学习之JSP基本知识篇
- Visual Studio使用技巧
- uva350 - Pseudo-Random Numbers
- Linux进程间通信——使用信号量
- 关于LWIP断开网线后重连问题(热插拔问题)
- POJ 2524 Ubiquitous Religions 并查集
- 缓存替换算法笔记 ——2Q
- 花生壳发布网站直接将网址绑定到具体的项目上——jboss版
- destoon实现调用热门关键字的方法
- 汉澳师攻城
- JNDI在tomcat中的配置
- Linux进程间通信——使用共享内存
- 工作日志2014-07-16
- 【腾讯开源】LivePool:基于Node.js的跨平台Web抓包替换工具