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: ...
随机推荐
- codeforces 629A Far Relative’s Birthday Cake
A. Far Relative’s Birthday Cake time limit per test 1 second memory limit per test 256 megabytes inp ...
- ASP.NET MVC程序传值方式:ViewData,ViewBag,TempData和Session
转载原地址 http://www.cnblogs.com/sunshineground/p/4350216.html 在ASP.NET MVC中,页面间Controller与View之间主要有以下几种 ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- AppServ 配置还是成功了
安装就不说了,一键式安装,还不错. 首先,登陆密码吧,用户是root ,密码是空: 之前创建的databases都还在, 可以用phpmyadmin管理,不在输入复杂的命令了, 方便,继续鼓捣php.
- C#的枚举数(Enumerator)和可枚举类型(Enumerable)
数组可以被foreach语句遍历数组中的元素,原因是数组可以按需提供一个叫做枚举数(enumerator)的对象.枚举数可以依次返回请求的数组的元素. 对于有枚举数的类型而言,必须有一个方法来获取它们 ...
- LFI漏洞利用总结(转载)
主要涉及到的函数include(),require().include_once(),require_once()magic_quotes_gpc().allow_url_fopen().allow_ ...
- java.io.File类
java.io.File类 1.凡是与输入.输出相关的类.接口等都定义在java.io包下 2.File是一个类.能够有构造器创建其对象.此对象相应着一个文件(.txt .avi .doc .ppt ...
- Wps的ppt里 让图片按顺序出现 就是点击一下 出现一张照片
基本操作能够用两种方法来实现: 方法一.每页幻灯片插入一张图片,幻灯片默认就是单击鼠标切换幻灯片的,所以不用再做其它设置. 方法二.在一页幻灯片中插入多张图片,全选图片(插入图片后,点击图片,Ctrl ...
- JAVA实现HTTPserver端
用java socket实现了一个简单的httpserver, 能够处理GET, POST,以及带一个附件的multipart类型的POST.尽管中途遇到了非常多问题, 只是通过在论坛和几个高手交流了 ...
- 学习JSONP
最近自己研究 跨域调用js,然后 发现 有jsonp 这种技术,在Jquery中可以使用,于是 研究下原理 发现: 其实 就是 利用<script>的跨域访问的能力. 调用 服务端 返回的 ...