The Suspects 并查集
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
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
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
int n,m,f[],k,ex[],c;
int init()
{
for(int i=;i<n;i++)
f[i]=i;
}
int getf(int x)
{
if(f[x]!=x)f[x]=getf(f[x]);
return f[x];
}
int merge(int x,int y)
{
int xx=getf(x),yy=getf(y);
f[yy]=xx;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
c=;
if(!m&&!n)break;
init();
for(int i=;i<m;i++)
{
scanf("%d",&k);
for(int i=;i<k;i++)
{
scanf("%d",&ex[i]);
if(i>)merge(ex[i-],ex[i]);
}
}
int fa=getf();
for(int i=;i<n;i++)
if(getf(i)==fa)c++;
cout<<c<<endl;
}
}
The Suspects 并查集的更多相关文章
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 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 ...
- B - The Suspects(并查集)
B - The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Desc ...
- 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 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- The Suspects(并查集求节点数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28164 Accepted: 13718 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
随机推荐
- ubuntu , 安装包的具体文件的查看方法
To see all the files the package installed onto your system, do this: dpkg-query -L <package_name ...
- ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点、线、圆,显示提示信息
ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点.线.圆,显示提示信息 /// <summary> /// 绘制界面上的点和线 /// ...
- Confluence 6 从外部目录中同步数据手动同步缓存
你可以通过单击用户目录(User Directories)界面中的同步(Synchronize)按钮,手动进行同步.如果一个同步进程已经正在同步的过程中的话,你就不能在上一个同步进程完成之前重新进行同 ...
- Oracle11g温习-第一章 2、ORACLE 物理结构
2013年4月27日 星期六 10:26 物理操作系统文件的集合.主要包括: 控制文件(参数文件init$ORACLE_SID.ora记录了控制文件的位置) 二进制文件,控制文件由参数control_ ...
- 字符串练习——唐纳德与子串 (Easy)
G1. 唐纳德与子串 (Easy) Time limit per test: 1.0 seconds Memory limit: 256 megabytes 子串的定义是在一个字符串中连续出现的一段字 ...
- OC 设计模式
设计模式 一种或几种被所有程序员广泛认同的,有某些特定功能,或者实现某些特殊作用的编码格式 单例模式 键值编码(KVC) 键值观察(KVO) 观察者模式() 工厂模式(工厂方法) ps:MVC &am ...
- 线程同步,条件变量pthread_cond_wait
与互斥锁不同,条件变量是用来等待而不是用来上锁的.条件变量用来自动阻塞一个线程,直到某特殊情况发生为止.条件变量使我们可以睡眠等待某种条件出现.条件变量是利用线程间共享的全局变量进行同步的一种机制,主 ...
- Python的数据类型3元组,集合和字典
首先要讲到的就是元组 元组其实拥有列表的一些特性,可以存储不同类型的值,但在某些方面元组又比不上列表 定义一个元组,你可以不用加‘ [ ] ’,你只需用逗号隔开即可 例如 1 2 3 4 5 6 7 ...
- STL标准库-仿函数与仿函数适配器
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 概要: 1.仿函数 2.bind2nd() 3.not1() 4.bind() 仿函数的实现:声明一个类,重载它的operato ...
- Arrays类的分析及使用
1. Arrays类介绍 Arrays类是Java API中提供的类,在java.util包中,此类包含用来操作数组的各种方法,比如排序和搜索,在这个类中如果指定数组引用为null,则此类方法都会抛 ...