题目链接: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. 802.11 wireless 三

    802.11 wireless 3watts,milliwatts,and Decibels瓦特(功率单位)的定义是1焦耳/秒微波炉1000瓦特,手机100-200毫瓦 decibels(分贝:比较能 ...

  2. JavaScript + HTML DOM (keep for myself)

    1.改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容 eg. <script>document.write(Date());</script> 绝对 ...

  3. Poj 2528 Mayor's posters 分类: Brush Mode 2014-07-23 09:12 84人阅读 评论(0) 收藏

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40570   Accepted: 11798 ...

  4. DEVICE DRAW VERTEX BUFFER TOO SMALL

    D3D11 WARNING #356 这个傻warning的意思看起来是说vertex buffer 太小了 描述是这样的: Vertex Buffer at the input vertex slo ...

  5. nodeJs 初探 ~

    今天晚上,开始时间了一下nodejs,跟着 Node入门 一步步的往下走.对node开发也有了初步的了解. 期间没有碰到什么问题,只有在最后的时候,碰到了几个问题.在这里记录一下: 1 . cross ...

  6. 制作一个可以给team所有成员用的开发者证书

    1. 将证书的private key导出一个.p12的文件给别人 2. 从apple developer网站下载证书对应的provisioning profile 待优化

  7. Codeforces Round #204 (Div. 2)->C. Jeff and Rounding

    C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. WinHex分析PE格式(2)&& 如何手动添加区段并运行区段

    由于这次文章内容比较多 所以写成DOC文档 为了复习所学的知识,我在原本的软件里试者手动加入区段 ,并写给大家分享,还试试者用LordPE加区段发现竟然失败了, 还是自己动手比较实在,完美运行. 利用 ...

  9. extern关键字的使用

    A.置于变量或者函数前,以标示变量或者函数的定义在别处,提示编译器遇到此变量和函数时在其他地方寻找其定义. B.可用来进行链接指定. 1.使用extern声明外部变量 1.1在一个文件内声明外部变量 ...

  10. ios frame、bound和center定义及使用场景总结

    frame:指的是视图在父视图的坐标系统中的大小和位置. bound:指的是视图在视图本身的坐标系统中的大小(位置起点是原点). center:指的是视图在父视图坐标系统中的中心点. frame和bo ...