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 ...
随机推荐
- Linux-PAM认证机制
http://www.cnblogs.com/marility/articles/9235522.html https://www.jianshu.com/p/342c05b51b7c https:/ ...
- Go语言图形界面开发:Go版GTK
https://www.cnblogs.com/tennysonsky/p/8433888.html package main import ( "os" "github ...
- Java通过复选框控件数组实现添加多个复选框控件
编写程序,通过复选框控件数组事先选择用户爱好信息的复选框,在该程序中,要求界面中的复选框数量可以根据指定复选框名称的字符串数组的长度来自动调节. 思路如下: 创建JPanel面板对象: 使用JPane ...
- Asp.net动态生成表单
control.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind=&quo ...
- 线程同步 –AutoResetEvent和ManualResetEvent
上一篇介绍了通过lock关键字和Monitor类型进行线程同步,本篇中就介绍一下通过同步句柄进行线程同步. 在Windows系统中,可以使用内核对象进行线程同步,内核对象由系统创建并维护.内核对象为内 ...
- mysql临时表产生的执行效率问题改进(转)
问题: 近日,线上MySQL查出一个慢sql,每次都要查询1000ms以上,严重影响用户体验 今得空去诊断一番,记录如下: sql原句: SELECT r.object_id AS cardId, c ...
- Elasticsearch 5.4.3 聚合分组
第一个分析需求:计算每个tag下的商品数量 GET /ecommerce/product/_search { "aggs": { "group_by_tags" ...
- Theme.AppCompat.Light的解决方法
style name=”AppBaseTheme” parent=”Theme.AppCompat.Light” 改为 改为 style name=”AppBaseTheme” parent=”and ...
- Installing Python Modules
Email: distutils-sig@python.org As a popular open source development project, Python has an active s ...
- mybatis的selectOne和selectList没有数据返回时的问题
1.使用mybatis的selectList方法,如果数据表中没有数据返回,则返回空集合[ ],而不会返回null,这是mybatis作的封装 @Override public List<Con ...