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 ...
随机推荐
- PHP数组的遍历
对于012345...这样的数组可以用for进行遍历 $arr=array('a','b','c','d','e'); for($key=0;$key<count($arr);$key++){ ...
- codevs 1080 线段树练习 CDQ分治
codevs 1080 线段树练习 http://codevs.cn/problem/1080/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 一行N个 ...
- 穷竭搜索:POJ 3187 Backward Digit Sums
题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...
- 五、Kafka 用户日志上报实时统计之应用概述
一.kafka 回顾 1.简介 Kafka 的业务 业务场景: 解除耦合 增加冗余 提高可扩展性 Buffering 异步通信 2.介绍 Kafka 的应用场景 Push Message Websit ...
- Java并发编程原理与实战三十五:并发容器ConcurrentLinkedQueue原理与使用
一.简介 一个基于链接节点的无界线程安全队列.此队列按照 FIFO(先进先出)原则对元素进行排序.队列的头部 是队列中时间最长的元素.队列的尾部 是队列中时间最短的元素.新的元素插入到队列的尾部,队列 ...
- Web开发中的18个关键性错误
前几年,我有机会能参与一些有趣的项目,并且独立完成开发.升级.重构以及新功能的开发等工作. 本文总结了一些PHP程序员在Web开发中经常 忽略的关键错误,尤其是在处理中大型的项目上问题更为突出.典型的 ...
- HDU 1171 Big Event in HDU(01背包)
题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...
- js鼠标自定移入输入框文本框光标自动定位到文本框
1.干货直接上 选中输入框设置如下: document.getElementById("Text1").focus();
- hdu 1716 排列2(DFS搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Time Limit: 1000/1000 MS (Java/Others) Me ...
- spring-boot-CommandLineRunner
在项目服务启动完成后就去加载一些数据 @Component public class MyStartupRunner1 implements CommandLineRunner { @Override ...