POJ2524——Ubiquitous Religions
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
题目大意:有n个学生,每个都有信仰的宗教。。。 有m组数据(i,j)表示i与j同学是同一个宗教的。 输出有多少个宗教。
解题思路:简单的并查集。
Code:
#include<stdio.h>
#include<math.h>
int fat[];
int find(int x)
{
if (x!=fat[x])
fat[x]=find(fat[x]);
return fat[x];
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx!=fy)
fat[fx]=fy;
}
int main()
{
int n,m,times=,i,t1,t2,cnt;
while (scanf("%d %d",&n,&m)!=EOF)
{
times++;
cnt=;
if (n==&&m==) break;
for (i=; i<=n; i++)
fat[i]=i;
for (i=; i<=m; i++)
{
scanf("%d %d",&t1,&t2);
join(t1,t2);
}
for (i=; i<=n; i++)
if (fat[i]==i) cnt++;
printf("Case %d: %d\n",times,cnt);
}
return ;
}
POJ2524——Ubiquitous Religions的更多相关文章
- poj2524 Ubiquitous Religions(并查集)
题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ2524 Ubiquitous Religions(并查集)
题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- POJ 2524 Ubiquitous Religions
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 20668 Accepted: ...
- Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 26678 Accepted: ...
- poj 2524 Ubiquitous Religions(宗教信仰)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 30666 Accepted: ...
- POJ 2524 :Ubiquitous Religions
id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...
随机推荐
- lex&yacc4
yacc: we cannt use the $$ value dirictly. we need get it irrotly;
- MQ队列
显示队列名dspmq 打开队列 runmqsc QMSAA 200-远程队列 dis qr(*) 显示所有队列 dis qr(saa_to_cips) all 显示队列参数 20-本地队列 查看队列深 ...
- Windows Phone 8.1 页面导航
1. Windows Phone 8.1 的应用框架 一个应用拥有 1 个 Window,一个 Window 包含 1 个 Frame,一个 Frame 包含 多个 Page. 获取 Frame 的方 ...
- 51nod1417 天堂里的游戏
---恢复内容开始--- 1417 天堂里的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 多年后,每当Noder看到吉普赛人,就会想起那个遥 ...
- wampsever在win10中安装扩展掉坑
1.必须要退出wampserver 2.php pecl + 3.wampserver 64 3.0.6
- 关于yum与源码安装的LAMP或LNMP网页直接显示空白页的问题?
学习LAMP.LNMP时,遇到很奇怪的问题就是:搭建完LAMP或LNMP环境后安装PHPweb程序时,Discuz和Wordpress打不开安装向导,直接显示空白页(PHPWind9.0除外),没有任 ...
- Django开发网站(四)
模型: 配置数据库 首先保证数据库已经安装,默认在Ubuntu下已经安装了sqlite3数据库,然后在项目名下的配置文件settings.py修改如下代码: 安装sqlite3 DATABASES = ...
- web.xml常用元素
web.xml文件是用来初始化配置信息:比如welcome页面.servlet.servlet-mapping.filter.listener.启动加载级别等.当你的web工程没用到这些时,你可以不用 ...
- MySQL的复制原理及配置
MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置,以及一些常见的问题. 一.复制的原理 MySQL 复制基于主服 ...
- StatusStrip状态栏控件
1.ToolStripStatusLabel statusstrip1.Items[].Text="日期"+DateTime.Now.ToString(); Thread p = ...