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的更多相关文章
随机推荐
- Java实现求二叉树的路径和
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...
- TCP协议三次握手和四次握手
前言 先说一下IP协议和TCP协议,IP协议是无连接的通信协议,IP不会占用两个设备之间通信的线路,IP实际上主要负责将每个数据包路由至目的地,但是IP协议并没有能够确保数据包是否到达,传过去的数据包 ...
- Python3 动手自己写谷歌翻译
本篇为实现谷歌翻译的功能,在编写的时候以为只是一个接口的问题. 没想到的是每次翻译都会触发一次JS的执行,在请求参数中生成一个tk. 文中tk的实现是复用的网上大神的代码生成tk. 好了,不说了直接看 ...
- MTK处理器手机 解锁Bootloader 教程
目前很多手机都需要解锁Bootloader之后才能进行刷机操作 本篇教程教你如何傻瓜式解锁Bootloader 首先需要在设置-关于手机 找到版本号(个别手机可能是内核版本号,甚至其他) 然后 快 ...
- win7打开网络看不到局域网的其他电脑
双击打开桌面上的“网络”,在打开的窗口中看不到局域网的其他电脑/计算机.以前都可以看到的.可能是没有开启网络发现的原因,可是我并没有关闭网络发现.不知,怎么回事? Windows7查看网络邻居要开启g ...
- iOS---开发实用传感器
传感器 1.什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 2.传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都 ...
- .net mvc 运行监控和错误捕捉
方法类 /// <summary> /// 运行监控类 /// </summary> [AttributeUsage(AttributeTargets.Class | Attr ...
- 自制无线共享工具C++源代码及创建过程
// wire.cpp : 定义控制台应用程序的入口点.// #include <iostream>#include <string.h>using namespace std ...
- ubuntu命令行使用ftp客户端
转载 本篇文章主要介绍在Ubuntu 8.10下如何使用功能强大的FTP客户端软件NcFTP. Ubuntu的源里为我们提供了FTP客户端软件NcFTP,可这款工具对新手来说不是很方便.本文介绍的是一 ...
- comm - 逐行比较两个已排序的文件
总览 (SYNOPSIS) ../src/comm [OPTION]... LEFT_FILE RIGHT_FILE 描述 (DESCRIPTION) 逐行比较 已排序的 文件 LEFT_FILE 和 ...