POJ1611-The Suspects-ACM
| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 23002 | Accepted: 11171 |
Description
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
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
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的更多相关文章
- poj1611 The Suspects(并查集)
题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- POJ1611 The Suspects (并查集)
本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量 学生编号1,2,3,4 ...
- POJ1611 The Suspects 并查集模板题
题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...
- poj1611 The suspects【并查集】
严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spreading-Y ...
- POJ1611(The Suspects)--简单并查集
题目在这里 关于SARS病毒传染的问题.在同一个组的学生是接触很近的,后面也会有新的同学的加入.其中有一位同学感染SARS,那么该组的所有同学得了SARS.要计算出有多少位学生感染SARS了.编号为0 ...
- Day 2 下午
[POJ 3468]A Simple Problem with Integers给定Q个数A1, ..., AQ,多次进行以下操作:1.对区间[L, R]中的每个数都加n.2.求某个区间[L, R]中 ...
- poj 1988 Cube Stacking (并查集)
题意:有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作: M x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上. C x : 问方块x下面有多少 ...
- 暑假集训(2)第二弹 ----- The Suspects(POJ1611)
B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:20000KB ...
- POJ1611:The Suspects(模板题)
http://poj.org/problem?id=1611 Description Severe acute respiratory syndrome (SARS), an atypical pne ...
随机推荐
- About using UTF-8 fields in MySQL
https://www.adayinthelifeof.nl/2010/12/04/about-using-utf-8-fields-in-mysql/ I sometimes hear: “make ...
- zabbix web场景模拟监控配置
一,zabbix2.2.3 VMware Vsphere exsi监控配置步骤, 1,添加监控主机 2,添加聚集macro;{$PASSWORD} = yoodo.com{$URL} = http:/ ...
- bzoj2822: [AHOI2012]树屋阶梯
咦,这里有好多东西https://en.wikipedia.org/wiki/Catalan_number 每个矩形最多贡献一个拐角 枚举左上角的点和那个拐角是一个矩形 #include<cst ...
- ubuntu下管理android手机
1.安装adb apt-get install android-tools-adb 2.配置 1)运行lsusb Bus 001 Device 002: ID 8087:0024 Intel Corp ...
- 怎样在delphi中实现控件的拖拽
下面这2种方法都能实现对控件和窗体的拖拽 方法1 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift ...
- [PWA] Keynote: Progressive Web Apps across all frameworks
PWA: Add to home screen Angular Universal Server side rendering: for achieving better proference on ...
- linux_2.6内核内存缓冲与I/O调度机制:
http://blog.csdn.net/kaiwii/article/details/7030178 到底是BIO还是BH?答案是BIO与BH
- 第三篇:GPU 并行编程的运算架构
前言 GPU 是如何实现并行的?它实现的方式较之 CPU 的多线程又有什么分别? 本文将做一个较为细致的分析. GPU 并行计算架构 GPU 并行编程的核心在于线程,一个线程就是程序中的一个单一指令流 ...
- java面试32问
第一,谈谈final, finally, finalize的区别. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements ...
- SQL Server中的20个系统变量
1.@@CONNECTIONS返回自上次启动 Microsoft SQL Server以来连接或试图连接的次数.示例:下面的示例显示了到当前日期和时间为止试图登录的次数.SELECT GETDATE( ...