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

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

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

AC代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. #define ma 300010
  5.  
  6. int fa[ma],m,n;
  7. int h[ma];
  8. int member[ma];
  9. int Count = 1;
  10. void init(int a)
  11. {
  12. for(int i = 0; i < a; i++)
  13. {
  14. fa[i] = i;
  15. h[i] = 0;
  16. }
  17. return;
  18. }
  19.  
  20. int Find(int x)
  21. {
  22. if(fa[x] == x)
  23. return x;
  24. else
  25. return fa[x] = Find(fa[x]);
  26. }
  27.  
  28. void unite(int x,int y)
  29. {
  30. x = Find(fa[x]);
  31. y = Find(fa[y]);
  32.  
  33. if(x == y) return;
  34. else
  35. {
  36. if(h[x] > h[y]) fa[y] = fa[x];
  37. else
  38. {
  39. fa[x] = fa[y];
  40. if(h[x] == h[y]) h[y]++;
  41. }
  42. }
  43. }
  44. bool same(int x, int y)
  45. {
  46. return Find(x) == Find(y);
  47. }
  48.  
  49. inline void solve(int n,int m)
  50. {
  51. init(n);
  52. while(m--)
  53. {
  54. int a;
  55. cin>>a;
  56. for(int i = 0; i < a; i++)
  57. cin>>member[i];
  58. //sort(member,member+a*sizeof(int));
  59. for(int i = 1; i < a; i++)
  60. unite(member[i-1],member[i]);
  61. }
  62. for(int i = 1; i < n; i++)
  63. if(same(0,i)) Count++;
  64. cout<<Count<<endl;
  65.  
  66. }
  67.  
  68. int main()
  69. {
  70. while(cin>>n>>m)
  71. {
  72. if(!n) break;
  73. solve(n,m);
  74. Count = 1;
  75. }
  76.  
  77. return 0;
  78. }

作者: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. 【POJ】【1821】Fence

    DP/单调队列优化 题意:k个人粉刷总长为n的墙壁(或者说栅栏?),每个人有一个必刷点s[i](这个人也可以一点也不刷,如果刷就必须刷这个点),最大粉刷长度l[i](必须是连续粉刷一段),和粉刷一格的 ...

  2. windows phone中ListBox的简单使用

    学习windows phone数据绑定的一点点心得,在wp系统的APP中经常遇到这样风格的软件,那它们到底怎样实现的呢?我就大致去做了一下,比较粗虐,但基本的都已经有了,实现后的结果为: 哇,这个图截 ...

  3. linux下解压命令大全(转载)

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...

  4. 移动端页面调试工具——UC浏览器开发者版

    在移动页面的开发中,我们很难像PC端那样很方便的调试,网上也有各种各样的调试方式.但在工作中,我主要还是用chorme自带的模拟器来模拟各种移动设备,但是用久了之后发现毕竟是模拟的,与真机调试还是会有 ...

  5. mysql存储过程和函数使用实例

    1.需求:根据输入的年份,月份,和当前系统的年份比较,不满1年按1年计算,多出1年11个月也按1年计算. 2.计算得出来的使用年份,计算车辆残值. 3.存储过程 DELIMITER $$ USE `d ...

  6. CSS 加载新方式

    Chrome 浏览器有意改变<link rel="stylesheet">的加载方式,当其出现在<body>中时,这一变化将更加明显.笔者决定在本文中进行详 ...

  7. Javascript Date类常用方法详解

    getDate :得到的是今天是 几号(1-28.29.30.31). getDay  :  得到的是今天是 星期几(1-7). getFullYear : 得到的是今天是 几几年(4位). getH ...

  8. CodeIgniter API

    http://apigen.juzna.cz/doc/EllisLab/CodeIgniter/tree.html Classes CI_Benchmark CI_Calendar CI_Cart C ...

  9. javascript console

    javascript console console.log(object[, object, ...])在控制台输出一条消息.如果有多个参数,输出时会用空格隔开这些参数. 第一个参数可以是一个包含格 ...

  10. ExtJs布局之table

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...