poj 1611 The Suspects(并查集输出集合个数)
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
题意:有n个学生 共m组 现在问你有多少个人和第0个学生一个组(包括0自己)
思路:并查集 并用一个数组记录当前节点下有多少子节点
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n,m;
int f[];
ll sum[];
int group[];
int find(int x){
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
void join(int x,int y){
int xx=find(x);
int yy=find(y);
if(xx!=yy){
f[xx]=yy;
sum[yy]+=sum[xx];
} }
int main(){
ios::sync_with_stdio(false);
while(cin>>n>>m){
if(n==&&m==) break;
for(int i=;i<n;i++)
f[i]=i,sum[i]=;
while(m--){
int temp;
cin>>temp;
for(int i=;i<=temp;i++)
cin>>group[i];
for(int i=;i<=temp-;i++)
if(find(group[i])==)
join(group[i+],group[i]);
else if(find(group[i+])==)
join(group[i],group[i+]);
else join(group[i+],group[i]);
}
cout<<sum[]<<endl;
}
return ;
}
poj 1611 The Suspects(并查集输出集合个数)的更多相关文章
- 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 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- 并查集(不相交集合)详解与java实现
目录 认识并查集 并查集解析 基本思想 如何查看a,b是否在一个集合? a,b合并,究竟是a的祖先合并在b的祖先上,还是b的祖先合并在a上? 其他路径压缩? 代码实现 结语 @(文章目录) 认识并查集 ...
- 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 ...
随机推荐
- Linux bc 命令简单学习
1. bash里面能够实现比较简单的四则运算 echo $((*)) 注意是 双括号+ $ 地址符号. 2. 但是比较复杂的 可能就难以为继了 比如不支持精度 3. 所以这里面需要使用 bc 命令来执 ...
- MyBatis映射文件5
返回map Map<String,Object> getEmpByResMap(Integer id); <select id="getEmpByResMap&qu ...
- Handler主线程子线程之间的互相通信
Handler主线程子线程之间的互相通信 package com.wyl.dansnote; import android.app.Activity; import android.os.Bundle ...
- Hbase存储模式
以key.value的结构存储数据; (table,rowkey,family,colum,timestamp)构成数据的key,value存储数据
- hive之size函数和cast转换函数
size返回map集合中元素的个数: cast函数将一种类型的数据转换成其他格式的数据
- 运维常用mysql语句
1..select @@version; ##查询当前mysql的版本. 2. show variables like 'port';##查看mysql实例的端口. 3.show variables ...
- python django 的环境搭建(centos)
一.安装好nginx 二.安装uwsgi yum install python-devel -y pip3 install uwsgi #测试启动django /usr/local/python3/b ...
- Spring Boot基础:Spring Boot简介与快速搭建(1)
1. Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的创建.运行.调试.部署等. Spring Boot默认使用tomca ...
- easyui combobox 在datagrid中动态加载数据
场景:datagrid 中用编辑框修改数据,有一个列使用的combobox 在可编辑的时候需要动态绑定数据,这个数据是在根据其他条件可变的 思路:在每次开启编辑框的时候动态绑定数据, datagri ...
- 安装zip版mysql
MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\P ...