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. 用cmd改计算机名.bat 无需重启电脑生效

    echo offset /p cname=请输入计算机名: echo REGEDIT4 >reg.reg echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentContr ...

  2. 0ull 是什么意思?

    unsigned long long 类型的0 同理:#define     DEV_IIC1             ( (u64)1 <<  7ULL)    这个作用是64位中的第7 ...

  3. MRTG开源监控安装手册

    环境要求: CentOS6以上版本 2G内存 16G存储 源码包下载:第一部分:下载,第二部分:下载 1.安装snmp: yum install -y net-snmp net-snmp-devel ...

  4. verilog 双向IO实现

    网上搜索了一番,示例挺多,但发现都写的是 input in; output out; 然后  assign io= (oe)?out:1'bz;就有点想不明白了,当IO方向为输出时,应该输出out的值 ...

  5. codeforces 421d bug in code

    题目链接:http://codeforces.com/problemset/problem/421/D 题目大意:每个人说出自己认为的背锅的两个人,最后大BOSS找两个人来背锅,要求至少符合p个人的想 ...

  6. Install MongoDB on Windows (Windows下安装MongoDB)

    Install MongoDB on Windows Overview Use this tutorial to install MongoDB on a Windows systems. PLATF ...

  7. ASP.NET与SOAP协议使用记录

    近期初次接手一个公司的管理系统开发任务,因为公司需要有Android,IOS客户端,又要求有PC端的网页客户端....对服务请求的要求自然也就落在了统一接口访问上了.... 使用ASP.NET的WEB ...

  8. (二)windows下安装PHPCMS V9

    一.准备工作 搭建环境 :参考:Windows下搭建PHP开发环境及相关注意事项 PHPCMS V9 :下载适合自己 PHPCMS V9 版本到本地或服务器,下载地址:http://www.phpcm ...

  9. linux操作系统cron详解

    Linux操作系统定时任务系统 Cron 入门 cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动 ...

  10. 【转】Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式

    http://www.cnblogs.com/wanqieddy/archive/2013/09/06/3305482.html android线程池的理解,晚上在家无事 预习了一下android异步 ...