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 小希的迷宫 基 ...
随机推荐
- [POI2007]Zap
bzoj 1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descriptio ...
- [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)
[NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...
- sql server 查询本周、本月所有天数的数据
查询本月所有的天数: --本月所有的天数 ),) day from (),,)+'-01' day) t1, ( ) t2 ),) ),,)+'%' 查询本周所有的天数: ),,),) ),,),) ...
- Python数据类型(整型,字符串类型,列表)
一:数据的概念 1.数据是什么 x=10,数据10就是我们要存储的数据. 2.为什么数据要分不同的种类? 因为数据是用来表示状态的,不同的状态就要用不同类型的数据去表示. 3:Python中常见的数据 ...
- 在Linode VPS上搭建离线下载神器Aria2+WEBUI管理及对国内云盘看法
在Linode VPS上搭建离线下载神器Aria2+WEBUI管理及对国内云盘看法 2015-09-21 by Hansen 原文链接:http://www.hansendong.me/archive ...
- Servlet笔记6--Servlet程序改进
第一步改进,GenericServlet: 我们目前所有放入Servlet类直接实现了javax.servlet.Servlet接口,但是这个接口中有很多方法是目前不需要的,我们可能只需要编写serv ...
- 【Python项目】爬取新浪微博签到页
基于微博签到页的微博爬虫 项目链接:https://github.com/RealIvyWong/WeiboCrawler/tree/master/WeiboLocationCrawler 1 实现功 ...
- mysql备份参数--master-data和--dump-slave的介绍
[mysql@db2 ~]$ mysqldump -A --master-data=2 > master2.sql[mysql@db2 ~]$ mysqldump -A --master-dat ...
- 25 个常用的 Linux iptables 规则【转】
转自 25 个常用的 Linux iptables 规则 - 文章 - 伯乐在线http://blog.jobbole.com/108468/ # 1. 删除所有现有规则 iptables -F # ...
- 使用pandas把mysql的数据导入MongoDB。
使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...