poj_2524_Ubiquitous Religions_201407211506
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 23390 | Accepted: 11527 |
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
Source
#include <stdio.h>
#include <string.h>
int pre[];
int find(int n)
{
int i,t;
t = n;
while(t!=pre[t])
t=pre[t];
while(t!=pre[n])
{
i = pre[n];
pre[n]=t;
n = i;
}
return t;
}
int main()
{
int n,m,k=;
while(scanf("%d%d",&n,&m),n||m)
{
int i,j,a,b,pa,pb,total=n;
//memset(s,0,sizeof(s));
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);
pa=find(a);
pb=find(b);
if(pa!=pb)
{
pre[pa]=pb;
total--;
}
}
printf("Case %d: ",k++);
printf("%d\n",total);
}
return ;
}
并差集的简单应用
poj_2524_Ubiquitous Religions_201407211506的更多相关文章
随机推荐
- Windowsforms 中 进程,线程
进程: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动. 它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体. Process 类,用来操作进程. 命名空间:using Syst ...
- 调用wsdl接口,参数是xml格式
1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...
- AJPFX总结IO流中的缓冲思想
缓冲思想 (因为内存的运算速度要远大于硬盘的原酸速度,所以只要降低硬盘的读写次数,就可以提高效率) 1. 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多, 2. 这是加 ...
- 纯CSS写的对勾样式
& .cicle{ position: relative; float: right; margin-right: -1rem; ...
- formSelects-v4.js 基于Layui的多选解决方案
https://hnzzmsf.github.io/example/example_v4.html
- C# 获取U盘ID序列号及U盘信息
C# 获取U盘ID序列号及U盘信息 2011-05-20 上传大小:35KB c#VS2005U盘IDU盘信息 获取U盘ID序列号 VS2005编译通过,源码源自CSDN.已经测试好用. 可以获得 ...
- 一个圆的移动 AE教程 速度曲线调节
AE里面速度的曲线调节 最终的小效果 两个关键点: 一:速度曲线调节 编辑速度图标,他的曲线是编辑速度的. 二:节点不要用贝塞尔曲线 编辑值图标,就是圆圈的x值y值的曲线.控制位置移动的. 选择一个节 ...
- php基础查找算法
1.顺序查找 function line_search($array,$tar) { if(!is_array($array) || count($array) < 1) return fals ...
- Redux的中间件Middleware不难,我信了^_^
Redux的action和reducer已经足够复杂了,现在还需要理解Redux的中间件.为什么Redux的存在有何意义?为什么Redux的中间件有这么多层的函数返回?Redux的中间件究竟是如何工作 ...
- jQuery-鼠标经过显示大图并跟随鼠标效果方法封装
//copyright c by zhangxinxu 2019-1-15 /*由于大图绑定在href属性中,故一般而言,需使用a标签的href指向大图.仅支持png,gif,jpg,bmp四种格式的 ...