[并查集] POJ 1611 The Suspects
| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 35206 | Accepted: 17097 |
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
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
Source
#include<stdio.h>
#include<string.h>
int father[30050],n;
void init()
{
int i;
memset(father,-1,sizeof(father));
for(i=0;i<n;++i) father[i]=i;
}
int find(int x)
{
if(x==father[x]) return (x);
else father[x]=find(father[x]);
return (father[x]);
}
void merge(int x,int y)
{
if(find(x)!=find(y)) father[find(x)]=find(y);
}
int main()
{
int m,i,qnum,x,pre,ans;
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
init();
while(m--)
{
scanf("%d",&qnum);
scanf("%d",&pre);
for(i=1;i<qnum;++i)
{
scanf("%d",&x);
merge(x,pre);
}
}
ans=0;
for(i=0;i<n;++i) if(find(i)==find(0)) ++ans;
printf("%d\n",ans);
}
return 0;
}
[并查集] POJ 1611 The Suspects的更多相关文章
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
随机推荐
- angular源码阅读3:真的,依赖注入的原理
前面已经提到了: 如何注册一个module. 如何获取一个module. injector与module以及provider的关系. 那么已经剩下最后一部分了,就是关于依赖是如何被注入的. 且看下面这 ...
- VS2013 带命令行参数的调试问题 解决方案
int main(int argc,char* argv[]) argc是命令行总的参数个数,argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数 比如: ...
- all ,any,abs的使用
#!/usr/bin/env python #all循环参数,如果每个元素都为真,那么all的返回值为真 r = all([True,'sad','asd']) print(r) #any 只有一个真 ...
- JavaScript的chapterIII
七.函数 函数由关键字function + 函数名 + 一组参数定义 函数可以被反复调用 语法: function funName( arg0,arg1,... argN){ statements; ...
- GOLANG SDK下载
如果没有FQ的话是不能访问国外网站的,但是golang提供了中国站点,要下载sdk可以在中国站点下载 中国站点: http://www.golangtc.com/download
- Android Button上的文字自动变成大写,如何解决呢?
android:textAllCaps="false"手动添加这一行,就不会有烦恼了.
- Stl源码剖析读书笔记之Alloc细节
阅读基础: Foo *pf = new Foo; 执行了两个步骤: 1)::operator new 向系统申请内存. 2) 调用Foo::Foo()构造函数构造实例. ==> 申请内存,构造 ...
- 【Java】异常处理_学习笔记
异常: 1.格式1: try { //业务代码 } catch(Exception e) { //异常处理代码 } 说明: a. 异常抛出:执行try里的代码,系统会自动生成一个异常对象,该对象会 ...
- 事务管理(下) 配置spring事务管理的几种方式(声明式事务)
配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...
- git 提交代码到github错误处理
git push -u origin mastererror: The requested URL returned error: 403 Forbidden while accessing http ...