POJ 1611:The Suspects(并查集)
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 48327 | Accepted: 23122 |
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个点集,每个点集有k个点,每个点集中的个点可以相连,求与0相连的点的个数(包括0)。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
int a[maxn];
int sum[maxn];
int ans;
int n,m;
int k,x,y;
int find(int x)
{
if(x!=a[x])
{
return find(a[x]);
}
return a[x];
}
void join(int x,int y)
{
int dx=find(x);
int dy=find(y);
//对dx(或dy)所在的点集进行个数统计
if(dx!=dy)
{
a[dy]=dx;
sum[dx]+=sum[dy];
}
}
int main(int argc, char const *argv[])
{
while(std::cin>>n>>m&&n||m)
{
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
ans=1;
for(int i=0;i<n;i++)
{
sum[i]=1;
a[i]=i;
}
while(m--)
{
std::cin>>k;
std::cin>>x;
k--;
while(k--)
{
std::cin>>y;
join(x,y);
}
}
//输出0所在点集的个数
std::cout<<sum[find(0)]<<std::endl;
}
return 0;
}
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 (并查集+数组记录子孙个数 )
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 ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- 并查集 (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: 35206 Accepted: 17097 De ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
随机推荐
- lua实现单例模式
Singleton = {} function Singleton:new(o) o = o or {} setmetatable(o,self) self.__index = self return ...
- 2016 CCPC 长春 Solution
A - Hanzo vs. Genji 留坑. B - Fraction 水. #include <bits/stdc++.h> using namespace std; inline i ...
- MapReduce 过程详解 (用WordCount作为例子)
本文转自 http://www.cnblogs.com/npumenglei/ .... 先创建两个文本文件, 作为我们例子的输入: File 1 内容: My name is Tony My com ...
- SQLServer cast()函数
语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQLServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的数据,在AS ...
- AVAudioFoundation(3):音视频编辑
本文转自:AVAudioFoundation(3):音视频编辑 | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 音视频编辑 上 ...
- c++第二十天
p111~p114: 与旧代码的接口 1.C++程序中的C风格字符串:任何出现字符串字面值的地方都可以以空字符结束的字符数组来替代. const char str[] = {"Hello, ...
- c++第二十四天
p126~p128: 1.关系运算符作用于算数类型和指针类型. 2.逻辑运算符作用于任意能转换成布尔类型值的类型. 3.以上两种运算的运算对象和运算结果都是右值. 4.逻辑与和逻辑或的运算策略:短路求 ...
- Maven聚合项目在eclipse中显示没有层次
大部分时间都在用idea做maven的项目,今天用eclipse导入了maven项目,果然不出所料,界面有显示问题,各个模块都堆叠在同一层级,根本看不出父项目与子项目之间的层次关系,见下图: 于是找修 ...
- JS实现弹出层效果
很多时候我们想去某某网站干点什么的时候,就会让我们先注册登录后才可以访问内容,而现在很多网站注册登录的时候都会有一种遮罩层的效果,就是背景是带有透明度的黑色遮罩,盖满整个网站,然后登录框弹出固定在屏幕 ...
- 如何解决ubuntu报的错误:You must put some 'source' URIs in your sources.list
答:添加deb-src开头的源,如 deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-updates multiverse