题目链接:http://poj.org/problem?id=1611

题意:输入n个人,m个组。初始化0为疑似病例。输入m个小组,每组中只要有一个疑似病例,整组人都是疑似病例。相同的成员可以在不同的组。找出一共有多少个疑似病例。

解题思路:同组的同parent,查找,合并集合。最后将出现的每个组员的parent和0的parent相比较,统计便可。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
#define ma 300010 int fa[ma],m,n;
int h[ma];
int member[ma];
int Count = 1;
void init(int a)
{
for(int i = 0; i < a; i++)
{
fa[i] = i;
h[i] = 0;
}
return;
} int Find(int x)
{
if(fa[x] == x)
return x;
else
return fa[x] = Find(fa[x]);
} void unite(int x,int y)
{
x = Find(fa[x]);
y = Find(fa[y]); if(x == y) return;
else
{
if(h[x] > h[y]) fa[y] = fa[x];
else
{
fa[x] = fa[y];
if(h[x] == h[y]) h[y]++;
}
}
}
bool same(int x, int y)
{
return Find(x) == Find(y);
} inline void solve(int n,int m)
{
init(n);
while(m--)
{
int a;
cin>>a;
for(int i = 0; i < a; i++)
cin>>member[i];
//sort(member,member+a*sizeof(int));
for(int i = 1; i < a; i++)
unite(member[i-1],member[i]);
}
for(int i = 1; i < n; i++)
if(same(0,i)) Count++;
cout<<Count<<endl; } int main()
{
while(cin>>n>>m)
{
if(!n) break;
solve(n,m);
Count = 1;
} return 0;
}

作者:u011652573 发表于2014-2-27 15:18:54 原文链接
阅读:54 评论:0 查看评论

[原]poj-1611-The Suspects(水并查集)的更多相关文章

  1. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  2. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  3. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  4. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  5. poj 1611 The Suspects(简单并查集)

    题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...

  6. POJ - 1611 The Suspects 【并查集】

    题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...

  7. POJ 1611 The Suspects【并查集】

    解题思路:一共给出 n个人,m组,接下来是m组数据,每一组开头是该组共有的人 num,则接下来输入的num个数,这些数是一组的 又因为最开始只有编号为0的人携带有病毒,且只有同一组的人会相互传染,问最 ...

  8. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  9. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  10. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

随机推荐

  1. UVALive - 6952 Cent Savings dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...

  2. JS 学习笔记--9---变量-作用域-内存相关

    JS 中变量和其它语言中变量最大的区别就是,JS 是松散型语言,决定了它只是在某一个特定时间保存某一特定的值的一个名字而已.由于在定义变量的时候不需要显示规定必须保存某种类型的值,故变量的值以及保存的 ...

  3. 5-Highcharts曲线图之轴反转

    <!DOCTYPE> <html lang='en'> <head> <title>5-Highcharts曲线图之轴反转</title> ...

  4. Drawing with GoogLeNet

    Drawing with GoogLeNet In my previous post, I showed how you can use deep neural networks to generat ...

  5. PowerDesigner(九)-模型文档编辑器(生成项目文档)(转)

    模型文档编辑器 PowerDesigner的模型文档(Model  Report)是基于模型的,面向项目的概览文档,提供了灵活,丰富的模型文档编辑界面,实现了设计,修改和输出模型文档的全过程. 模型文 ...

  6. Java递归列出目录下全部文件

    Java递归列出目录下全部文件 /** * 列出指定目录的全部内容 * */ import java.io.*; class hello{ public static void main(String ...

  7. 【JQuery NoviceToNinja系列】01 开篇 Html页面设计和布局

    01 开篇 Html页面设计和布局 index.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml ...

  8. 禁用backspace键的后退功能

    禁用backspace键的后退功能,但是可以删除文本内容<script language="JavaScript">document.onkeydown = check ...

  9. 集合类 Collection

    1.Collection接口有两个子接口: List:保存元素顺序的线性表,允许有重复元素. Set:不记录元素的保存顺序,不允许有重复元素.数学中的集合 Collection接口中的方法如下: Co ...

  10. Project Euler P105:Special subset sums: testing 特殊的子集和 检验

    Special subset sums: testing Let S(A) represent the sum of elements in set A of size n. We shall cal ...