poj1611 The Suspects(并查集)
题目链接
http://poj.org/problem?id=1611
题意
有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔离。起初学生0是疑似患者,求要隔离多少人。
思路
使用并查集求解。
代码
#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] = i;
} int find_root(int x)
{
if (x == p[x])
return x;
else
{
int temp = find_root(p[x]); //路径压缩
p[x] = temp;
return temp;
}
} void union_set(int x, int y)
{
int px = find_root(x);
int py = find_root(y);
if (px != py)
p[px] = py;
} int main()
{
freopen("poj1611.txt", "r", stdin);
int n, m;
while (scanf("%d%d", &n,&m)== && n)
{
make_set(n);
for (int i = ; i < m; i++)
{
int k;
int x, y;
scanf("%d%d", &k, &x);
for (int j = ;j < k;j++)
{
scanf("%d", &y);
union_set(x, y);
x = y;
}
}
int root = find_root();
int ans = ;
for (int i = ; i < n; i++)
if (find_root(i) == root)
ans++;
cout << ans << endl;
}
return ;
}
poj1611 The Suspects(并查集)的更多相关文章
- POJ1611 The Suspects 并查集模板题
题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 De ...
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- B - The Suspects(并查集)
B - The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Desc ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj1611 带权并查集
题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
随机推荐
- 模块型css样式
<div id="dowork"> <div id="dowork_on">۞作业进行中</div> <div id= ...
- webApp 页面适配布局
webApp 页面布局 1. 流式布局 概念: 流式布局是页面元素宽度按照屏幕分辨率进行适配调整,但是整体布局不变. 设计方法: 布局都是通过百分比来定义宽度,但是高度大都是用px固定的. 弊端: 虽 ...
- eclipse插件之Findbugs、Checkstyle、PMD安装及使用
eclipse插件之Findbugs.Checkstyle.PMD安装及使用 一.什么是Findbugs.checkstyle.PMD Findbugs.checkstyle和PMD都可以作为插件插入 ...
- [USACO07NOV]挤奶的时间Milking Time
https://daniu.luogu.org/problemnew/show/2889 按右端点从小到大排序后DP dp[i] 到第i个时间段的最大产奶量 不能按左端点排序,第i段由第j段更新时,第 ...
- MySQL学习(一)——Java连接MySql数据库
MySQL学习(一)——Java连接MySql数据库 API详解: 获得语句执行 String sql = "Insert into category(cid, cname) values( ...
- c++ 前置++与后置++的区别
用C++编程的都知道,C++提供了一个非常强大的操作符重载机制,利用操作符重载,我们可以为我们自定义的类增加更多非常有用的功能.不过,C++也有限制,就是当我们为自定义的类重载操作符时,重载操作符的含 ...
- idea注册码激活防和谐
1.到网站 http://idea.lanyus.com/ 获取注册码: 2.修改hosts文件,位于C:\Windows\System32\drivers\etc,添加一行,win10推荐使用not ...
- dup()&dup2()
[dup()&dup2()] 都是复制文件描述符指针.dup2可以指定复制到哪一个新索引. 参考:http://hi.baidu.com/flikecn/item/e82e14bef06e8a ...
- [转载]解决flash与js交互、flash跨域交互、flash跨域提交
http://blog.csdn.net/andyxm/article/details/5219919 我们引用本地flash,实现flash与js双向交互. function thisMovie(m ...
- c++刷题(12/100)无序数组中和为定值的最长子数组
题目一: 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...