Ubiquitous Religions 【poj-2524】【并查集】
来源:互联网 发布:国信蓝点java培训 编辑:程序博客网 时间:2024/06/07 07:22
Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 36790 Accepted: 17517
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<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=50005;int par[maxn]; //存父亲节点int rank[maxn]; //存树的高度void init(int n){ //初始化for(int i=1;i<=n;i++){par[i]=i;rank[i]=0;}}int find(int x){ //查询树的根if(par[x]==x){return x;}else{return par[x]=find(par[x]);}}void unite(int x,int y){ //合并x=find(x);y=find(y);if(x==y) return ;if(rank[x]<rank[y]){par[x]=y;}else{par[y]=x;if(rank[x]==rank[y]) rank[x]++;}}int main(){int n,m;int k=1;while(scanf("%d%d",&n,&m)){if(n==0&&m==0) break;init(n);int a,b;for(int i=0;i<m;i++){scanf("%d%d",&a,&b);unite(a,b);}int num=0;for(int i=1;i<=n;i++){if(par[i]==i) num++;}printf("Case %d: %d\n",k++,num);}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 并查集
- ios-CALayer
- 深度学习:卷积神经网络CNN
- IDEA tomcat问题
- C++词法记号规则之标识符 关键字 操作符 分隔符 空白符
- Math.random()
- Ubiquitous Religions 【poj-2524】【并查集】
- H
- 新手学C++多线程编程(2)进程
- JEESZ-Zookeeper集群安装
- java基础第一课
- jeesz分布式架构 Dubbo、zookeeper、KafKa、redis、fastdfs、单点登录sso、springmvc+mybatis+shiro、Restful服务
- ZZULIOJ【1069】向Z同学学习【循环】&&【模拟】
- Js中的 封装
- 前端知识积累(可能包含面试题)