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 ...
随机推荐
- 取值为[1,n-1]含n个元素的整数数组,至少存在一个重复数,即可能存在多个重复数,O(n)时间内找出其中任意一个重复数,不使用额外存储空间。
有一种非常诡异的算法,就是采用类似于单链表是否存在环的问题.“判断单链表是否存在环”是一个非常经典的问题,同时单链表可以采用数组实现,此时每个元素值作为next指针指向下一个元素.本题可以转换化为“已 ...
- 5 -- Hibernate的基本用法 --1 1 对象/关系数据库映射(ORM)
ORM的全称是Object/Relation Mapping ,即对象/关系数据库映射.ORM可理解成一种规范,它概述了这类框架的基本特征:完成面向对象的编程语言到关系数据库的映射.当ORM框架完成映 ...
- C#中的Abstract、Virtual、Interface理解
容易混淆是必须的,都是与继承有关系,并且涉及到override的使用 一.Virtual方法(虚方法) virtual 关键字用于在基类中修饰方法.virtual的使用会有两种情况: 情况1:在基类中 ...
- 重命名和重定向RF执行生成的output文件
命令: pybot -d output文件保存路径 -o output文件重命名 -d --outputdir dir Where to create output files. The defaul ...
- linux 设置分辨率(转)
linux 设置分辨率 如果你需要在linux上设置显示屏的分辨率,分两种情况:分辨率模式存在与分辨率模式不存在,具体如下. 1,分辨率模式已存在 1)如何查询是否存在: 图形界面:在System S ...
- Windows下POSIX线程编程(pThread)环境搭建
系统: Windows 编辑器:codeblocks13.12 1. 简介: Windows有一个叫 POSIX Threads for Win32 的开源项目给出了一个功能比较完善的Windows下 ...
- PowerDesigner学习笔记
首先是概念模型个物理模型的区别,概念模型,即ConceptualDataMode,是没和具体类型的数据库(如SQL Server.MySql)的对应,是一种抽象的,而物理模型是指定了某种具体类型的数据 ...
- JBuilder+struts一个常见异常
[org.apache.commons.digester.Digester]-[ERROR] Parse Error at line 3 column 22: The content of eleme ...
- 【推荐系统论文笔记】Introduction To Recommender Systems: Algorithms and Evaluation
这篇论文比较短,正如题目所说,主要还是简单地介绍了一下推荐系统的一些算法以及评估的方法. 推荐系统之前是基于关键字信息的过滤系统,后来发展成为协同过滤系统,解决了两个问题:1.通过人工审核去评价那些具 ...
- [转]C++中模板的特化与偏特化
转载自:http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html 1.引言C++中的模板分为类模板和函数模板,虽然它 ...