poj 2524 Ubiquitous Religions(宗教信仰)
| Time Limit: 5000MS | Memory Limit: 65536K | |
|
Total Submissions: 30666 |
Accepted: 14860 |
Description
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
Output
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
Hint
#include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
int f[]; int find(int x) {
if (x != f[x])
f[x] = find(f[x]);
return f[x];
} void Union(int a, int b) {
int f1 = find(a);
int f2 = find(b);
if (f1 != f2)
f[f2] = f1;
} int main() {
//ifstream cin("aaa.txt");
int n, m, test = , sum; while(scanf("%d%d", &n, &m)){
if (n == && m == )
break;
memset(f, , sizeof(f)); for(int i = ; i <= n; i++) {
f[i] = i;
} sum = n; for(int i = ; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
if(find(a) != find(b)){
Union(a, b);
sum--;
}
} printf("Case %d: %d\n", test++, sum); }
//system("pause");
return ;
}
#include <stdio.h>
#include <iostream>
using namespace std; const int MAXN = ; /*结点数目上线*/
int pa[MAXN]; /*p[x]表示x的父节点*/
int rank1[MAXN]; /*rank[x]是x的高度的一个上界*/
int n, ans; void make_set(int x)
{/*创建一个单元集*/
pa[x] = x;
rank1[x] = ;
} int find_set(int x)
{/*带路径压缩的查找*/
if(x != pa[x])
pa[x] = find_set(pa[x]);
return pa[x];
} /*按秩合并x,y所在的集合*/
void union_set(int x, int y)
{
x = find_set(x);
y = find_set(y);
if(x == y)return ;
ans--; //统计
if(rank1[x] > rank1[y])/*让rank比较高的作为父结点*/
{
pa[y] = x;
}
else
{
pa[x] = y;
if(rank1[x] == rank1[y])
rank1[y]++;
}
}
//answer to 2524
int main()
{
int m, i, j = , x, y;
while(scanf("%d%d", &n, &m))
{
if(n == m && m == ) break;
for(i = ; i <= n; i++)
make_set(i);
ans = n;
for(i = ; i < m; i++)
{
scanf("%d%d", &x, &y);
union_set(x, y);
}
printf("Case %d: %d\n", j, ans);
j++;
}
return ;
}
poj 2524 Ubiquitous Religions(宗教信仰)的更多相关文章
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- POJ 2524 Ubiquitous Religions
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 20668 Accepted: ...
- POJ 2524 Ubiquitous Religions 解题报告
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34122 Accepted: ...
- [ACM] POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23093 Accepted: ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- POJ 2524 Ubiquitous Religions (并查集)
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
- POJ 2524 Ubiquitous Religions (幷查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23090 Accepted: ...
随机推荐
- Hibernate HQL和原生SQL查询的一点区别
1.createSQLQuery 1.1默认查询的结果为BigDecimal 1.2通过addScalar("CGD_ID", StandardBasicTypes.LONG)可以 ...
- DataSource , DataSink, DataSourceLoop
Script assertion in login:
- andriod开发,简单的封装网络请求并监听返回.
一.为什么封装 因为android 4.0 以后的发送网络请求必须要放到异步线程中,而异步线程必须跟handle合作才能更新主线程中的UI,所以建议用一个类继承handler来异步处理网络请求. 二. ...
- 错误号码2003 Can't connect to MySQL server 'localhost' (0)
错误描写叙述 错误原因 近期,我一直都能够用SQLyog连接本地数据库,可是近几天却无法连接:而且一直都报上述错误,我查阅了非常多资料,发现有非常多中说法 总结一下 第一,MySQL中的my.ini出 ...
- android SoundPool播放音效
MediaPlayer的缺点: 资源占用量高,延时时间较长 不支持多个音效同一时候播放 SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音 ...
- .NET中的三种Timer的区别和用法
最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应 ...
- Java 装饰模式 (Decorator)
装饰模式 动态的将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的取代方案 代码 package gx.component; /** * 组件:装饰类和被装饰类 都要继承:为了类型保持一致 ...
- [MODx] 2. Install some useful packages into ur MODx
1. The package we might need: 2. Install the package: Select Installer Download Extras Install the p ...
- Android横竖屏切换处理
Android横竖屏要解决的问题应该就两个: 1.布局问题:2.重新载入问题 一.布局问题: 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你 ...
- IDispatch接口 - GetIDsOfNames和Invoke(转)
IDispatch接口是COM自动化的核心.其实,IDispatch这个接口本身也很简单,只有4个方法: IDispatch : public IUnknown { public: virtual H ...