poj1611 The suspects【并查集】
Input
Output对于每组测试数据, 输出一行可能的患者。
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
输出和0所在集合的元素个数
合并之和遍历一次就行了
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 30005;
int t, n, m;
int ran[maxn], parent[maxn];
bool vis[maxn];
void init(int n)
{
for(int i = 0; i < n; i++){
ran[i] = 0;
parent[i] = i;
vis[i] = false;
}
}
int fin(int x)
{
if(x == parent[x]) return x;
int t = parent[x];
parent[x] = fin(parent[x]);
return parent[x];
}
void mer(int x, int y)
{
int tx = fin(x);
int ty = fin(y);
if(tx != ty){
parent[tx] = ty;
ran[tx] = ran[ty] + 1;
}
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF && (n != 0 || m != 0)){
init(n);
for(int i = 0; i < m; i++){
int before;
scanf("%d%d", &t, &before);
for(int j = 1; j < t; j++){
int now;
scanf("%d", &now);
mer(now, before);
before = now;
}
}
int cnt = 0;
for(int i = 0; i < n; i++){
if(fin(0) == fin(i)){
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
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 ...
随机推荐
- SpringBoot------8080端口被占用抛出异常
异常信息: The Tomcat connector configured to listen on port failed to start. The port may already be in ...
- 5 -- Hibernate的基本用法 --3 Hibernate的体系结构
⊙ SessionFactory : 这是Hibernate的关键对象,它是单个数据库映射关系经过编译后的内存镜像,也是线程安全的.它是生成Session的工厂,本身需要依赖于ConnectionPr ...
- RF使用ie浏览器访问页面,浏览器启动只显示This is the initial start page for the WebDriver server,页面访问失败
问题描述:启动ie浏览器后,页面显示如下: 问题定位: 1.IE页面缩放没有设置成100% 2.ie浏览器的安全模式设置是否都将“启动保护模式”勾选上 3.iedriver驱动版本号是否和seleni ...
- 怎么打乱List中元素的顺序
使用Collections类中shuffle随机打乱List内部元素顺序 原文地址:http://blog.csdn.net/warren2013/article/details/17414771 / ...
- 网站前端性能优化之javascript和css
之前看过Yahoo团队写的一篇关于网站性能优化的文章,文章是2010年左右写的,虽然有点老,但是很多方面还是很有借鉴意义的.关于css的性能优化,他提到了如下几点: CSS性能优化 1.把样式表置于顶 ...
- JDBC批量执行executeBatch
JDBC事务 在数据库中,所谓事务是指一组逻辑操作单元,使数据从一种状态变换到另一种状态.为确保数据库中数据的一致性,数据的操纵应当是离散的成组的逻辑单元:当它全部完成时,数据的一致性可以保持,而当这 ...
- System.Web.Mvc.AJAX缺少程序集引用
今天在构建新项目的时候,出现这个错误,实际上这个出现过很多次了,但是每次都忘记之前怎么解决的,之前博客也不记得写过没有...
- GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403
使用 git 的命令行向 GitHub 提交的时候,报错: [Young@localhost OtherLang]$ git push origin master error: The request ...
- C C++ 文件输入与输出
C语言: 一 打开关闭文件 1 fopen函数 用于打开文件 FILE *fopen(char *filename, *type); fopen("c:\\ccdos\\clib" ...
- Android ADB命令?这一次我再也不死记了!【简单说】
https://www.jianshu.com/p/56fd03f1aaae adb的全称为Android Debug Bridge.是android司机经常用到的工具.但是问题是那么多命令写代码已经 ...