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. Linux process state codes

    Here are the different values that the s, stat and state output specifiers (header "STAT" ...

  2. hdu 4403 枚举

    #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...

  3. MySQL可视化管理工具 —— Navicat for MysSQL

    类似PL/SQL管理Oracle的工具 Navicat可以管理MySQL 1.安装 2.连接(输入IP.端口.用户名.密码) 3.新建sql语句:点击左侧database.点击菜单“查询”.点击“新建 ...

  4. 【转】服务器证书安装配置指南(Weblogic)

    服务器证书安装配置指南(Weblogic) 详情请点击: http://verisign.itrus.com.cn/html/fuwuyuzhichi/fuwuqizhengshuanzhuangpe ...

  5. poj 2462 Period of an Infinite Binary Expansion

    欧拉定理.根据分数转换成2进制的过程,分子每次都乘2.对于循环节x,当2^x = 1(mod b)时肯定是循环节.显然当分母不能整除2的时候,即分母和2互质的话,就可以利用欧拉定理,使得2^(Eule ...

  6. 【设计模式 - 7】之过滤器模式(Filter)

    1      模式简介 过滤器模式(Filter)也叫标准模式(Criteria),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来. 2      实例 需求 ...

  7. 由 argv引出的main参数 分类: C/C++ 2014-11-08 18:00 154人阅读 评论(0) 收藏

    我们经常用的main函数都是不带参数的.因此main 后的括号都是空括号.实际上,main函数可以带参数,这个参数可以认为是 main函数的形式参数.C语言规定main函数的参数只能有两个, 习惯上这 ...

  8. Qt 学习之路:Qt 简介

    Qt 是一个著名的 C++ 应用程序框架.你并不能说它只是一个 GUI 库,因为 Qt 十分庞大,并不仅仅是 GUI 组件.使用 Qt,在一定程度上你获得的是一个“一站式”的解决方案:不再需要研究 S ...

  9. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  10. [转] 「指尖上的魔法」 - 谈谈 React Native 中的手势

    http://gold.xitu.io/entry/55fa202960b28497519db23f React-Native是一款由Facebook开发并开源的框架,主要卖点是使用JavaScrip ...