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的更多相关文章
随机推荐
- Webform 内置对象 Session对象、Application全局对象,ViewState
Session 每台电脑访问服务器,都有独立的session,key值都一样,内容不一样. 1.session保存在服务器上. 2.session没有持久性,保存周期就是20分钟. 重点: sessi ...
- Windows下的一个Nginx 批处理命令行控制台
其实作用很简单,就是为了少输入几个字母,完成对Nginx的控制而已,同时也算是练习了一把bat批处理吧. @echo off&color e&Title Nginx 命令行控制台 cl ...
- STM32 (基础语法)笔记
指针遍历: char *myitoa(int value, char *string, int radix){ int i, d; int flag = 0; char *ptr = string; ...
- debian设置英文模式
Ubuntu太臃肿了,遂换回debian系统.在虚拟机上装debian,发现console下中文不显示. 各种export方法试过,始终无效.废了一个小时终于找到方法了.记录之. debian设置语言 ...
- Windows bat 设置代理
转自tt-0411 @echo off cls color 0A Echo The program is running... Echo Setting the ip and dns... netsh ...
- Ryubook_1_switch_hub_源码
一.switching hub by openflow: 用Ryu实现一个有如下功能的switching hub. • Learns the MAC address of the host conne ...
- ztree 展开一级节点 | ztree只显示到二级目录
// 默认展开一级节点var nodes = tree.getNodesByParam("level", 0);for (var i = 0; i < nodes.lengt ...
- xamarin.forms 绑定页面里指定元素的某个属性值
{Binding Source={x:Reference elementName}, Path=BindingContext.propertyname, Mode=OneWay} elementNam ...
- CAD交互绘制虚线(com接口)
用户可以在控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY ...
- CAD使用GetxDataLong读数据(com接口)
主要用到函数说明: MxDrawEntity::GetxDataLong2 读取一个Long扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, retval ...