POJ 1611 The Suspects (并查集+数组记录子孙个数 )
| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 24134 | Accepted: 11787 |
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
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
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>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#define N 30000 using namespace std;
int n, m;
int cnt[N];
int fa[N]; //0 < n <= 30000 and 0 <= m <=500
void init()
{
for(int i=; i<N; i++)
{
fa[i]=i;
cnt[i]=;
}
} int findset(int x)
{
return fa[x]!=x? fa[x]=findset(fa[x]):x;
} void union_set(int x, int y)
{
int xx=findset(x);
int yy=findset(y);
if(xx==yy) return; //说明两元素本来就属于同一个集合 返回
else if(xx<yy) //如果x的根节点比y的根节点 小
{
fa[yy]=xx;
cnt[xx]=cnt[xx]+cnt[yy];
}
else if(xx>yy)
{
fa[xx]=yy;
cnt[yy]=cnt[yy]+cnt[xx];
}
} int main()
{
int dd, a, b;
while(scanf("%d %d", &n, &m)!=EOF)
{
if(n== && m==) break;
init();
for(int i=; i<m; i++)
{
scanf("%d", &dd);
scanf("%d", &a);
for(int j=; j<dd-; j++)
{
scanf("%d", &b);
union_set(a, b);
a=b;
}
}
printf("%d\n", cnt[] );
}
return ;
}
POJ 1611 The Suspects (并查集+数组记录子孙个数 )的更多相关文章
- 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 (并查集求数量)
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 ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
随机推荐
- KeyStore和TrustStore
笔者的这篇文章参考了http://docs.oracle.com/cd/E19509-01/820-3503/ggfgo/index.html KeyStore和TrustStore在很多HTTPS双 ...
- Network | DHCP
动态主机设置协议(Dynamic Host Configuration Protocol, DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途: 给内部网络或网络服务供应商自动分配I ...
- python3:requests模块-写了一点
使用requests,它的七个主要方法,在这里只讲两个:get.post >>> import requests >>> r=requests.get(" ...
- 断路器Hystrix与Turbine集群监控-Spring Cloud学习第三天(非原创)
文章大纲 一.Hystrix基础介绍二.断路器Hystrix简单使用三.自定义Hystrix请求命令四.Hystrix的服务降级与异常处理五.Hystrix的请求缓存与请求合并六.Hystrix仪表盘 ...
- Codeforces A. Bear and Big Brother
...不行.这题之后.不做1000分以下的了.很耻辱 A. Bear and Big Brother time limit per test 1 second memory limit per t ...
- JDK1.8中的Lambda表达式和Stream
1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有python或者javascript的语言基础,对理解Lambda表达式有很大帮助,因为Java正 ...
- sed命令2
测试文件sedtest.txt,内容为: [root@localhost sed]# cat sedtest.txt my cat's name is betty it's a cat; this i ...
- javascript 对象初探 (六)--- call()和apply()初探
在javascript中,每个函数都具有call()和apply()两个方法,您可以用她们来触发函数,并指定相关的调用参数. 此外,这两个方法还有另一个功能,就是她可以让一个对象去‘借用‘另一个对象的 ...
- tensorflow搭建神经网络基本流程
定义添加神经层的函数 1.训练的数据2.定义节点准备接收数据3.定义神经层:隐藏层和预测层4.定义 loss 表达式5.选择 optimizer 使 loss 达到最小 然后对所有变量进行初始化,通过 ...
- 分布式服务框架选型:面对Dubbo,阿里巴巴为什么选择了HSF?
转载:http://www.sohu.com/a/141490021_268033 阿里巴巴集团内部使用的分布式服务框架 HSF(High Speed Framework,也有人戏称“好舒服”)已经被 ...