poj2524 Ubiquitous Religions(并查集)
题目链接
http://poj.org/problem?id=2524
题意
有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a、b,表示同学a和同学b有相同的信仰,求在n名学生中最多存在多少种不同的宗教信仰。
思路
使用并查集解决。
代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = + ;
int p[N]; void make_set(int n)
{
for (int i = ;i <= n;i++)
p[i] = -;
} int find_root(int i)
{
if (p[i] == -)
return i;
else
{
int temp = find_root(p[i]); //路径压缩
p[i] = temp;
return temp;
}
} void union_set(int a, int b)
{
int ra = find_root(a);
int rb = find_root(b);
if (ra != rb)
p[ra] = rb;
} int main()
{
//freopen("poj2524.txt", "r", stdin);
int n, m;
int cnt = ;
while (scanf("%d%d", &n, &m) == && n)
{
make_set(n);
int a, b;
for (int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
union_set(a, b);
}
int ans = ;
for (int i = ;i <= n;i++)
if (p[i] == -) ans++;
printf("Case %d: %d\n", ++cnt, ans);
}
return ;
}
poj2524 Ubiquitous Religions(并查集)的更多相关文章
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
- POJ2524 Ubiquitous Religions(并查集)
题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...
- [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: 23090 Accepted: ...
- poj 2524 Ubiquitous Religions (并查集)
题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ2524——Ubiquitous Religions
Ubiquitous Religions Description There are so many different religions in the world today that it is ...
- poj2524(简单并查集)
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- Nlog写日志到数据库
https://github.com/nlog/NLog/wiki/Database-Target
- RTSP服务器之————rtsp-server(轻量级RTSP / RTP流媒体服务器)
github:https://github.com/revmischa/rtsp-server 轻量级RTSP / RTP流媒体服务器
- Django 2.0.1 官方文档翻译: 编写你的第一个 Django app,第三部分(Page 8)
编写你的第一个 Django app,第三部分(Page 8)转载请注明链接地址 本页教程接前面的第二部分.我们继续开发 web-poll app,我们会专注于创建公共接口上 -- "视图& ...
- GitHub更新已经fork的项目
clone 自己的 fork 分支到本地 可以直接使用 GitHub 客户端,clone 到本地,如果使用命令行,命令为: $ git clone git@github.com:morethink/g ...
- Linux下编译Phantomjs
1.安装依赖的库 <pre> sudo apt-get install g++ flex bison gperf ruby perl \ libsqlite3-dev libfontcon ...
- WPF控件收集
1.Extended WPF Toolkit 2.Fluent Ribbon Control Suite 3.WPF Ribbon Control 4.Telerik RadControls for ...
- 13、Math类简介
Math类概述 在java.lang包下,有个Math类,这个类包含用于执行基本数学运算的方法,如四舍五入,开方等等. package com.sutaoyu.usually_class; publi ...
- summernote 文本编辑器使用时,选择上传图片、链接、录像时,弹出的对话框被遮挡住
更多内容推荐微信公众号,欢迎关注: 即问题如下链接内的情况: http://bbs.csdn.net/topics/392004332 这个一般属于CSS中样式出现了问题,可以在点开的时候,F12查看 ...
- async语法升级踩坑小记
从今年过完年回来,三月份开始,就一直在做重构相关的事情. 就在今天刚刚上线了最新一次的重构代码,希望高峰期安好,接近半年的Node.js代码重构. 包含从callback+async.waterfal ...
- JavaScript验证注册信息
<script language="javascript"> function check_login(form){ if(form.username.value==& ...