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 ...
随机推荐
- pom.xml 配置maven私服
1.pom.xml 配置maven私服 <repositories> <repository> <id>caf_repositories& ...
- Python--异常处理--12
Python 异常处理 原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ python提供了两个非常重要的功能来处理python程序在运行中出现的 ...
- HybridApp启动引导页的实现
有一种帅叫做长话短说,@孙红雷,--这可以叫做“短帅”吗,^_^ 首先说下思路,既然是Hybrid APP, 那就是可以用html的方式实现,启动引导页比较常见的展示方式是滑动,那么我们就可以使用图片 ...
- 8 -- 深入使用Spring -- 2... Spring的“零配置”支持
8.2 Spring的“零配置”支持 Spring支持使用Annotation来代替XML配置文件.
- phpVirtualBox – 用浏览器操作虚拟机
摘自:https://code.google.com phpVirtualBox 一个开源的,VirtualBox的用户界面,用PHP编写的AJAX实现.作为一个现代的Web界面,它允许你远程访问和控 ...
- ios开发之 -- NSString指定字体高亮显示
一个简单的小需求,就是在一个字符串里面,指定一部分字节高亮显示,代码如下: NSString *descStr = @"需要高亮显示的字符"; NSString *nickStr ...
- Spring容器AOP的理解
一句话理解:根据被代理对象信息通过Proxy动态生成我们具体的代理类. 实现就动态代理.那动态代理是什么呢? 动态代理其实并不是什么新鲜的东西,学过设计模式的人都应该知道代理模式,代理模式就是一种静态 ...
- Linux应急响应(三):挖矿病毒
0x00 前言 随着虚拟货币的疯狂炒作,利用挖矿脚本来实现流量变现,使得挖矿病毒成为不法分子利用最为频繁的攻击方式.新的挖矿攻击展现出了类似蠕虫的行为,并结合了高级攻击技术,以增加对目标服务器感染 ...
- Android开发-- 使用ADT23 的一些问题
在使用最新版ADT 23进行android学习时发现一些问题: 1.通过设置intent的action来启动另外一个activity时,会出现No Activity found to handle I ...
- [Python] Unofficial Windows Binaries for Python Extension Packages
1. Unofficial Windows Binaries for Python Extension Packages 非官方的Python第三方库,提供基于Windows的二进制扩展包,由加州大学 ...