并查集-POJ-1611-The Suspects
2016-01-31 23:20
387 查看
The Suspects
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 29598 Accepted: 14400
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
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
Source
Asia Kaohsiung 2003
题意:
有n个学生,m个小组,先给出分组情况,0号学生是SARS嫌疑人,但凡与SARS嫌疑人分在一个组,都会使自己成为嫌疑人,现求所有SARS嫌疑人人数。
题解:
其实就给出了连通情况,求与0号连通的点有多少个而已。并查集很好做。
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 29598 Accepted: 14400
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
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
Source
Asia Kaohsiung 2003
题意:
有n个学生,m个小组,先给出分组情况,0号学生是SARS嫌疑人,但凡与SARS嫌疑人分在一个组,都会使自己成为嫌疑人,现求所有SARS嫌疑人人数。
题解:
其实就给出了连通情况,求与0号连通的点有多少个而已。并查集很好做。
// // main.cpp // 并查集-B-The Suspects // // Created by 袁子涵 on 16/1/31. // Copyright © 2016年 袁子涵. All rights reserved. // // 16ms 892kb #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <stdlib.h> #include <vector> using namespace std; int n,m,k,first; int set[30005]; int stk[30005]; int find(int num) { int tmp=0,now=num; while (set[now]!=now) { stk[tmp++]=now; now=set[now]; } while (tmp) set[stk[--tmp]]=now; return now; } void join(int num1,int num2) { int f1=find(num1),f2=find(num2); set[f1]=set[f2]; } int main(int argc, const char * argv[]) { while (scanf("%d %d",&n,&m)&& (n!=0 || m!=0)) { int total=0; for (int i=0; i<n; i++) set[i]=i; for (int i=1; i<=m; i++) { int tmp; scanf("%d",&k); if (k==0) { continue; } scanf("%d",&first); for (int j=1; j<k; j++) { scanf("%d",&tmp); join(first, tmp); } } for (int i=0; i<n; i++) { if (find(i)==set[0]) { total++; } } cout << total << endl; } return 0; }
相关文章推荐
- Java设计模式-工厂模式(1)简单工厂模式
- 传说中的车 UVa11134
- 【HDOJ】4122 Alice's mooncake shop
- 将DataTable中多条记录的两列拼接显示
- poj2421Constructing Roads(最小生成树)
- ios编程之ReactiveCocoa框架
- 《C语言程序设计》指针篇<一>
- TexWatcher的用法
- bzoj4403 序列统计 组合
- 2015年编程历程回顾
- 最短路算法(Floyd、Dijsktra、Bellman-Ford、SPFA)
- [剑指offer-1384] 二维数组中的查找
- 重温JSP学习笔记--与日期数字格式化有关的jstl标签库
- SynchronousQueue、LinkedBlockingQueue、ArrayBlockingQueue性能测试(转)
- tomcat监控
- hdu 1028 母函数模版
- PHP foreach循环 总结
- 【自制插件】MMD4Maya
- android sdk tencent link
- self.edgesForExtendedLayout=UIRectEdgeNone--将原点移动到navigationBar下面