(easy) Ubiquitous Religion (P2524)
来源:互联网 发布:网络检测报告 编辑:程序博客网 时间:2024/05/16 17:41
#include<iostream>//#include<algorithm>using namespace std;//freopen("C://i.txt","r",stdin);#define N 50001int f[N];int n,m;int ans[N];int find(int k){if (k==f[k])return k;return f[k]=find(f[k]);}int main(){freopen("C://i.txt","r",stdin);int i,j,k;int num=0;while (cin>>n>>m,n+m){num++;for (i=1;i<=n;i++)f[i]=i;memset(ans,0,sizeof(ans));while (m--){scanf("%d%d",&i,&j);int x=find(i);int y=find(j);f[x]=y;}for (i=1;i<=n;i++){ans[find(i)]=1;}k=0;for (i=1;i<=n;i++)k+=ans[i];cout<<"Case "<<num<<": ";cout<<k<<endl;}}
Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 17490 Accepted: 8502
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
- (easy) Ubiquitous Religion (P2524)
- POJ P2524 Ubiquitous Religions
- POJ P2524 Ubiquitous Religions
- POJ P2524 Ubiquitous Religions
- POJ P2524 Ubiquitous Religions
- POJ P2524 Ubiquitous Religions 基础并查集
- Lain Layer04: Religion- -
- open source religion
- How Religion Destroys Programmers
- How Religion Destroys Programmers--ref
- true religion wholesale Adventures made accessible
- Ubiquitous Religions
- Ubiquitous Religions
- Ubiquitous Religions
- Ubiquitous Religions
- Ubiquitous Religions
- Ubiquitous Religions
- Ubiquitous Religions
- 庆祝访问量排名突破2W
- 让你的Lable拥有qq表情!
- C#的abstract关键字
- 图形开发绘图教程
- Python函数式编程指南(四):生成器(转载)
- (easy) Ubiquitous Religion (P2524)
- 扔骰子,求概率
- 待看的文章
- 孙鑫视频之菜单(下)
- lucene索引结构(六)--词位置(.prx)倒排索引文件结构分析
- 上下文和依赖关系注入以及 JSF 2.0 入门指南
- 算法设计与分析课程Part1笔记(5)
- GIT操作手则
- jsp实现的简单注册登录