POJ1161——The Suspects

 
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 48220   Accepted: 23072

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个团体,(0 < n <= 30000 , 0 <= m <= 500) 一个学生可以属于多个团体。 一个学生疑似患病,则它所属的整个团体都 疑似患病。已知0号学生疑似患病,以及每个 团体都由哪些学生构成,求一共多少个学生 疑似患病。
 
解法:
 
最基础的并查集,把所有可疑的都并一 块。
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #define N 4000000
  4. #define LL long long
  5. #define RE register
  6. #define IN inline
  7.  
  8. using namespace std;
  9.  
  10. IN void in(int &x) {
  11. RE char c=getchar();x=;int f=;
  12. while(!isdigit(c)) {if(c=='-') f=-;c=getchar();}
  13. while(isdigit(c)) {x=x*+c-'';c=getchar();}
  14. x*=f;
  15. }
  16.  
  17. int n,m,fa[N],w[N],anss,ans;
  18.  
  19. int find(int x){return fa[x]==x?x:find(fa[x]);}
  20.  
  21. int main()
  22. {
  23. while(){
  24. in(n);in(m);
  25. if(n==&&m==) return ;
  26. for(int i=;i<=n;i++) fa[i]=i;
  27. for(int i=;i<=m;i++){
  28. int tp;
  29. in(tp);
  30. for(int j=;j<=tp;j++){
  31. int fx,fy;in(w[j]);
  32. fx=find(w[j]);
  33. for(int k=;k<j;k++){
  34. fy=find(w[k]);
  35. if(fx!=fy) fa[fx]=fy;
  36. }
  37. }
  38. }ans=find();anss=;
  39. for(int i=;i<=n;i++){
  40. if(find(i)==ans) ++anss;
  41. }printf("%d\n",anss);
  42. }return ;
  43. }

  

 

POJ1161——The Suspects的更多相关文章

  1. [并查集] POJ 1611 The Suspects

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

  2. poj-1611-The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 34284   Accepted: 16642 De ...

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

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

  4. DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects

    题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...

  5. Poj1611The Suspects

    A - The Suspects Time Limit: 1000 MS Memory Limit: 20000 KB 64-bit integer IO format: %I64d , %I64u  ...

  6. poj 1611 The Suspects 并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30522   Accepted: 14836 De ...

  7. LA 4126 Password Suspects

    问题描述:给定m个模式串,计数包含所有模式串且长度为n的字符串的数目. 数据范围:模式串长度不超过10,m <= 10, n <= 25,此外保证答案不超过1015. 分析:既然要计数给定 ...

  8. POJ 1611 The Suspects (并查集)

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

  9. 暑假集训(2)第二弹 ----- The Suspects(POJ1611)

    B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:20000KB   ...

随机推荐

  1. Android获取全部存储卡挂载路径

    近期因项目需求.须要在存储卡查找文件,经測试发现部分手机挂载路径查找不到,这里分享一个有效的方法. /** * 获取全部存储卡挂载路径 * @return */ public static List& ...

  2. WPF学习笔记:获取ListBox的选中项

    有代码有J8: UI <UserControl x:Class="UnitViews.UserListUV" xmlns="http://schemas.micro ...

  3. 1245 最小的N个和(前k小ai+bi)

    1245 最小的N个和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 有两个长度为 N ...

  4. 在Sql Server触发器中判断操作是Insert还是Update还是Delete

    在Sql Server触发器中判断操作是Insert还是Update还是Delete DECLARE    @IsInsert bit,    @IsUpdate bit,    @IsDelete  ...

  5. bzoj2844

    http://www.lydsy.com/JudgeOnline/problem.php?id=2844 线性基... 先把线性基搞出来,然后不断逼近答案,如果这个基比答案小了,那么说明要加上,同时加 ...

  6. Mysql中date,time,datetime,timestamp的区别

    区别: timestamp:时间戳.北京时间1970年01月01日08时00分00秒 起至现在的总秒数. datetime:带时分秒的完整时间,例如:1970-01-01 10:00:00 date: ...

  7. [App Store Connect帮助]二、 添加、编辑和删除用户(3)添加和编辑用户

    您可以在“用户和访问”中添加和编辑用户. 如果您以个人名义在“Apple 开发者计划”注册,您可以授权用户访问您 App Store Connect 中的内容.所有用户只拥有 App Store Co ...

  8. JSP执行原理图

  9. MVC系列学习(十七)-过滤器

    本次学习的文件结构如下 1.过滤器的几种表示方式 1.1将过滤器 加到方法上,作用范围为该方法 1.2将过滤器加到当前类上,作用范围为该类的所有方法 1.3添加全局过滤器,作用范围为所有方法 2.Ac ...

  10. 6.11---swagger文件上传的写法【照着写就行了,主要是需要声明contentType未mutilpart---如果不设置这个,就无法识别文件的】

    MultipartFile 是直接接收前台传过来的文件,File是抽象出来的文件对象,用来表示文件,一般操作都是操作的File,所以需要将MultipartFile转为File controller写 ...