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: ...
随机推荐
- 需要熟记的git命令
需要熟记的github常用命令 总结一下ubuntu下github常用的命令,设置部分跳过,假设repository的名字叫hello-world: .创建一个新的repository: 先在gith ...
- C#Windows窗体界面设计_01_绘制三角函数_附强制类型转换
binzhouweichao@163.com 今天开始学习C#windows窗体界面设计. 首先说一下类型转换. 参考http://www.csharpwin.com/csharpspace/6848 ...
- Objective-c setObject:forKey:和setValue:forKey:的区别
setObject:forKey: 是NSMutableDictionary类的方法 key参数类型可以是任意类型对象 ...
- libevent的使用方法--回显服务器的简单实例
#include <event.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...
- 插入三层treeview代码
#region treetView加载 private void treeViewLoad() { DataView dv = navds.tbSiteKind.AsDataView(); treeV ...
- ECSHOP数据表结构完整仔细说明教程
From:http://www.ecshop119.com/ecshopjc-868.html s_account_log //用户账目日志表 字段 类型 Null 默认 注释 log_id medi ...
- sgu - 274 - Spam-filter
题意:判断一个字符串是不是有效的邮件地址. 题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=274 ——>>照着题目做. ...
- Hash Table构建
get-item e:\test\* |format-table @{name="aa";expression={$_.name.tostring().split(".& ...
- java中的hashcode和euqals的区别和联系
一.equals方法的作用 1.默认情况(没有覆盖equals方法)下equals方法都是调用Object类的equals方法,而Object的equals方法主要用于判断对象的内存地址引用是不是同一 ...
- TChromeTabs 优化改进
已知未解决问题 全屏时当窗体失去焦点,则会显示出未绘制完成的原标题栏(Fixed): 处于非 Areo 效果下时,窗体标题栏需要定制. 新增按钮上的 Hint 提示后再移至其它标签,将无法重新提示. ...