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

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. 
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). 
Once a member in a group is a suspect, all members in the group are suspects. 
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. 
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

  1. 100 4
  2. 2 1 2
  3. 5 10 13 11 12 14
  4. 2 0 1
  5. 2 99 2
  6. 200 2
  7. 1 5
  8. 5 1 2 3 4 5
  9. 1 0
  10. 0 0

Sample Output

  1. 4
  2. 1
  3. 1

Source

 
原题大意:对于两个整数N,M,N表示有N个人,M表示被分成了多少组。0号有传染病,和他一组的人都有嫌疑得传染病,对于每组数据,问有多少人有嫌疑。
 
解题思路:裸的并查集,可以用于练习模板。
  1. #include<stdio.h>
  2. #include<string.h>
  3. int father[30050],n;
  4. void init()
  5. {
  6. int i;
  7. memset(father,-1,sizeof(father));
  8. for(i=0;i<n;++i) father[i]=i;
  9. }
  10. int find(int x)
  11. {
  12. if(x==father[x]) return (x);
  13. else father[x]=find(father[x]);
  14. return (father[x]);
  15. }
  16. void merge(int x,int y)
  17. {
  18. if(find(x)!=find(y)) father[find(x)]=find(y);
  19. }
  20. int main()
  21. {
  22. int m,i,qnum,x,pre,ans;
  23. while(~scanf("%d%d",&n,&m))
  24. {
  25. if(n==0&&m==0) break;
  26. init();
  27. while(m--)
  28. {
  29. scanf("%d",&qnum);
  30. scanf("%d",&pre);
  31. for(i=1;i<qnum;++i)
  32. {
  33. scanf("%d",&x);
  34. merge(x,pre);
  35. }
  36. }
  37. ans=0;
  38. for(i=0;i<n;++i) if(find(i)==find(0)) ++ans;
  39. printf("%d\n",ans);
  40. }
  41. return 0;
  42. }

  

[并查集] POJ 1611 The Suspects的更多相关文章

  1. 并查集 (poj 1611 The Suspects)

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

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

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

  3. POJ 1611 The Suspects (并查集)

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

  4. poj 1611 The Suspects(并查集)

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

  5. poj 1611 The Suspects(并查集输出集合个数)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  6. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

  7. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  8. POJ 1611 The Suspects (并查集求数量)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  9. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

随机推荐

  1. command 'x86_64-linux-gnu-gcc' failed with exit status 1错误及解决方案

    Ubuntu16.04安装Scrapy(pip install Scrapy)时提示错误如下: Failed building wheel for cryptography Running setup ...

  2. 20161106PM-接口

    使用网址:http://apistore.baidu.com apikey:1f1d014aee0adeddbe33a6e1f55f7925 Composer GET http://apis.baid ...

  3. FMDB

    一.FMDB简介 1.FMDB简介 iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较繁琐.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB. ...

  4. python 最大公约数

    求解两个整数(不能是负数)的最大公约数(要求两数不能同时为0)当两数都是0时,最大公约数为0方式一:穷举法 def GCU(m, n): if not m: return n elif not n: ...

  5. test homework2 ~ faulty program

    Program1:

  6. 【Java】异常处理_学习笔记

    异常: 1.格式1: try { //业务代码 } catch(Exception e) { //异常处理代码 } 说明: a.   异常抛出:执行try里的代码,系统会自动生成一个异常对象,该对象会 ...

  7. HTML基本标签

    h1-h6:标题标签.(从大到小) p:段落标签. img:图片标签:属性src:图片的相对路径:alt:图片加载失败的提示语言. a:超链接标签:属性:href:地址链接:target:网页打开的默 ...

  8. js URL中文传参乱码

    js: var searchVal = encodeURIComponent($.trim($('#js_search_val').val()));//搜索的值 encodeURIComponent( ...

  9. welcome-file-list设置问题之css,js文件无法加载

    web.xml里的welcome-file-list里设置默认访问页面为/html/index.html 但是在访问时,页面CSS都没加载. 正常输入网址却没问题.用/html/index.jsp也没 ...

  10. Load Runner录制C/S客户端

    1.    打开应用程序 2.    点击如下菜单 弹出窗口如下 3.    点击New,弹出窗口如下,选择Web(HTTP/HTML) 4.    点击Create,弹出窗口 5.    点击OK, ...