The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 23002   Accepted: 11171

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

提交一直出现Wrong Answer,主要代码出错在union_set方法。

之前

void union_set(int x,int y){
x = find_set(x);
y = find_set(y);
if(rank[x] > rank[y]){
pa[y] = x;
count[x] += count[y];
}
else{
pa[x] = y;
if(rank[x] == rank[y]){
rank[y]++;
}
count[y] += count[x];
} }

修改为

void union_set(int x,int y){
x = find_set(x);
y = find_set(y);
if(x==y) return;
if(rank[x] > rank[y])
{
pa[y] = x;
count[x] += count[y];
rank[x]++;
}
else
{
pa[x] = y;
rank[y]++;
count[y] += count[x];
}
}

通过了

#include <cstdio>

const int MAXN = 30001;
int pa[MAXN];
int rank[MAXN];
int count[MAXN]; void make_set(int x){
pa[x] = x;
rank[x] = 0;
count[x] = 1;
} int find_set(int x){
if(x != pa[x]){
pa[x] = find_set(pa[x]);
}
return pa[x];
} void union_set(int x,int y){
x = find_set(x);
y = find_set(y);
if(x==y) return;
if(rank[x] > rank[y])
{
pa[y] = x;
count[x] += count[y];
rank[x]++;
}
else
{
pa[x] = y;
rank[y]++;
count[y] += count[x];
} } int main(void){
int n,m;
while(1){
int i;
scanf("%d%d",&n,&m);
if(0==n && 0==m){
break;
}
for(i=0;i<n;i++){
make_set(i);
}
for(i=0;i<m;i++){
int k,first,j;
scanf("%d%d",&k,&first);
for(j=1;j<k;j++){
int next;
scanf("%d",&next);
union_set(first,next);
}
}
int p = find_set(0);
printf("%d\n",count[p]);
}
return 0;
}

POJ1611-The Suspects-ACM的更多相关文章

  1. poj1611 The Suspects(并查集)

    题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...

  2. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  3. POJ1611 The Suspects (并查集)

    本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量  学生编号1,2,3,4 ...

  4. POJ1611 The Suspects 并查集模板题

    题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...

  5. poj1611 The suspects【并查集】

    严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spreading-Y ...

  6. POJ1611(The Suspects)--简单并查集

    题目在这里 关于SARS病毒传染的问题.在同一个组的学生是接触很近的,后面也会有新的同学的加入.其中有一位同学感染SARS,那么该组的所有同学得了SARS.要计算出有多少位学生感染SARS了.编号为0 ...

  7. Day 2 下午

    [POJ 3468]A Simple Problem with Integers给定Q个数A1, ..., AQ,多次进行以下操作:1.对区间[L, R]中的每个数都加n.2.求某个区间[L, R]中 ...

  8. poj 1988 Cube Stacking (并查集)

    题意:有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作: M x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上. C x : 问方块x下面有多少 ...

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

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

  10. POJ1611:The Suspects(模板题)

    http://poj.org/problem?id=1611 Description Severe acute respiratory syndrome (SARS), an atypical pne ...

随机推荐

  1. Nova 无法向虚机注入密钥

    欢迎各位关注我的博客:http://weibo.com/u/216633637 废话开头: 之前参考这位同学的博客http://www.cnblogs.com/awy-blog/p/3447176.h ...

  2. CentOS7.0分布式安装HADOOP 2.6.0笔记-转载的

    三台虚拟机,IP地址通过路由器静态DHCP分配 (这样就无需设置host了). 三台机器信息如下 -      1. hadoop-a: 192.168.0.20  #master     2. ha ...

  3. mysql inner join,full outer join,left join,right jion

    https://sites.google.com/site/349624yu/courses/mysql/mysqldbgjzcx inner join,full outer join,left jo ...

  4. eclipse的svn插件安装方式

    eclipse的插件安装一般有3种方式: 1)通过eclipse的Help/ Install New Software...中, 点击Add, 添加一个在线更新地址,如:http://subclips ...

  5. uva10622 Perfect P-th Powers

    留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...

  6. sql 视图 按where条件多个字段取一个 分类: SQL Server 2014-12-01 14:09 308人阅读 评论(0) 收藏

    首先介绍一下 Case ..When...Then..End  的用法: CASEJiXiaoFind_RowID  WHEN '1' THENJiXiao_Money1  WHEN '2' THEN ...

  7. javascript弹出框打印某个数值时,弹出NaN?(not a number)

    一.NaN:表示not a number null 未定义或空字符串 undefined 对象属性不存在 或是声明了变量但从未赋值. 二.出现这种情况有(1)此常数的值是零被零除所得到的结果. (2) ...

  8. [置顶] 获取激活码,激活myeclipse

    myeclipse10.0 正式版下载地址: http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/instal ...

  9. Java NIO——Selector机制源码分析---转

    一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java   Oper ...

  10. svs 在创建的时候 上传文件夹 bin obj 这些不要提交

    svs  在创建的时候 上传文件夹 bin  obj  这些不要提交  右键-去除版本控制并增加到忽略列表