poj-The Suspects
来源:互联网 发布:淘宝导购平台有哪些 编辑:程序博客网 时间:2024/05/08 11:00
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.
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.
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.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
For each case, output the number of suspects in one line.
100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0
411
这道题也是简单的考察并查集
输入人时第一个人不管,从第二个开始,与前面一个连成一个集合
这几天状态十分不好!
一个小毛病能WA好长时间
#include<stdio.h>int pre[30010];int stu[30010];int son[30010];int find(int a){ while(pre[a]!=a) { a=pre[a]; } return a;}void join(int i,int j){ int root1,root2; root1=find(i); root2=find(j); if(root1!=root2) { pre[root2]=root1; son[root1]+=son[root2]; }}int main(){ int n,m,num,T; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) { break; } for(int i=0; i<n; i++) { pre[i]=i; son[i]=1; } //T=m; for(int i=1; i<=m; i++) { scanf("%d",&num); for(int i=0; i<num; i++) { scanf("%d",&stu[i]); if(i!=0) { join(stu[i-1],stu[i]); } } } printf("%d\n",son[find(0)]); } return 0;}
总结:
1.要细心!!!自己WA了很长时间仅仅是因为find函数写错了!自己检查了很多遍也米有检查出来
int find(int a){ if(id[a]!=a) { a=id[a]; } return a;}
我被自己蠢到了!这样的find函数能用吗?两题都是在非常的简单的地方出错,卡了很长时间
2.刚开始要找有多少人时,我用的方法是一个一个遍历一遍,如果find(i)=find(0)时num++;
我去!好蠢的方法!这样30000个数又要遍历一遍?不怕超时?
其实再合并两个集合的时候我们就可以找一个数组来记录各组的数据
如果合并集合了,那么这两集合的元素个数加起来就行
等到用的时候就找到find(0)后再在存集合个数的数组里找集合有几个元素!
0 0
- POJ 1611 The Suspects
- poj 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ-1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 - The Suspects
- poj 1611.The Suspects
- POJ 1611 The Suspects
- Poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 the suspects
- 20个很棒的Android开源项目帮助你提升开发技能
- Volley网络请求概述
- LeetCode392.Is Subsequence题解
- object的notify、wait对线程使用的理解
- leetcode_middle_82_17. Letter Combinations of a Phone Number
- poj-The Suspects
- android中SELINUX规则分析和语法简介
- 迅速秒杀99%的海量数据处理面试题
- ulua入门1
- 用smarty模板做的登录
- src或者css背景图的url值为base64编码代码
- 数据压缩实验一实验报告
- js轮子—模态框组件
- 人工智能会统治人类吗?