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 ...
随机推荐
- windows环境下批处理实现守护进程
这个脚本提供两种方式守护,一种是通过进程名,但对于进程名都是java.exe的java程序不适用,另一种是通过netstat查找应用程序所监听的端口是否正在被监听.这两种方式可以在脚本中通过两个冒号注 ...
- bzoj千题计划171:bzoj2456: mode
http://www.lydsy.com/JudgeOnline/problem.php?id=2456 任意删除序列中两个不同的数,众数仍然是众数 不停的删,剩下的最后的数一定是众数 具体实现: 记 ...
- 从零搭建SSM框架(一)搭建工程
工程结构 一.cnki-parent 1.新建maven project 2.pom.xml <project xmlns="http://maven.apache.org/POM/ ...
- soj1046. Plane Spotting
1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond o ...
- Activity相关知识点总结
一.Activity状态 Activity有三种状态:active/running.paused.stopped. 1.active/running状态,在当前屏幕时,即用户可见的Activity,位 ...
- Bresenham直线算法与画圆算法
在我们内部开发使用的一个工具中,我们需要几乎从 0 开始实现一个高效的二维图像渲染引擎.比较幸运的是,我们只需要画直线.圆以及矩形,其中比较复杂的是画直线和圆.画直线和圆已经有非常多的成熟的算法了,我 ...
- springboot中报异常Whitelabel Error Page
开始以为是url写错了,但其实不是,然后启动application类在的包是要在最顶部,并且和pom中groupid一样 这个也没错,后来发现能访问RestController中的url,但是进不了方 ...
- aarch64_m2
mingw32-leptonica-1.74.4-1.fc26.noarch.rpm 2017-06-12 17:20 1.0M fedora Mirroring Project mingw32-le ...
- Python之容器类Collections
容器类Collections 标签(空格分隔): Python进阶 defaultdict counter deque namedtuple defaultdict defaultdict的作用是可以 ...
- quartz的一个误导
quartz文档提到,如果在集群环境下,最好将配置项org.quartz.jobStore.txIsolationLevelSerializable设置为true 问题: 这个选项在mysql下会非常 ...