POJ - 1611 The Suspects 【并查集】
题目链接
http://poj.org/problem?id=1611
题意
给出 n, m
有n个人 编号为 0 - n - 1
有m组人 他们之间是有关系的
编号为 0 的人是 有嫌疑的
然后和 编号为0的人有关系 或者 和 编号为0的人有关系的人 有关系的 都是有嫌疑的
找出共有多少人有嫌疑
思路
将所有有关系的人 合并 然后 去查找 所有的人的根节点 如果和编号为0的人的根节点 相同 他就是有关系的
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int MOD = 1e9 + 7;
int pre[maxn];
int find(int x)
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
}
void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) && (n || m))
{
init();
int Max = -INF;
for (int i = 0; i < m; i++)
{
int k;
int ini, num;
scanf("%d%d", &k, &ini);
Max = max(Max, ini);
for (int i = 1; i < k; i++)
{
scanf("%d", &num);
Max = max(num, Max);
join(ini, num);
}
}
int ans = 1;
int vis = find(0);
for (int i = 1; i <= Max; i++)
{
if (find(i) == vis)
ans++;
}
cout << ans << endl;
}
}
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 ...
随机推荐
- PHP 5.4 中经 htmlspecialchars 转义后的中文字符串为空的问题
PHP 5.4.3 环境中测试了一个在 PHP 5.2 环境下运行正常的程序,却发现本应正常提交一个中文字符串到数据库的代码却提交了一个空字符串,经过排查,该字符串在经 htmlspecialchar ...
- 一张图,关于 Bayes error rate,贝叶斯错误率等的分析
造轮子是那帮搞研究的“科学家”干的事情(类似E&E里的explore),“工程师”的职责是利用已有的东西解决问题(类似E&E里的exploit). 其次,即使以工程师的角色解决业务问题 ...
- 搭建k8s集群的手顺
https://www.cnblogs.com/netsa/category/1137187.html
- C#自定义MessageBox 按钮的Text
运行效果: 代码: using System; using System.Drawing; using System.Runtime.InteropServices; using System.Tex ...
- SQLServer-----SQLServer 2008 R2安装
- RMQ(区间求最值)
1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A.回答若干询问RMQ(A,i,j)(i,j<=n).返回数列A ...
- Bootstrap 模态框、轮播 结合使用
Bootstrap 模态框和轮播分开使用的教程网上非常多.可是两者结合使用的样例和资料非常少. 两者结合使用时,開始我遇到了不少bug,如今分享给大家. 我的这个样例是把图片轮播嵌入到模态框里. 最后 ...
- hdu4405Aeroplane chess 概率dp水题
//从0到n有n+1个格子 //对于格子i,掷一次骰子的数为x.那么能够从位置i到位置i+x //格子之间有连线,假设格子a和b有连线,那么从a到b不用掷骰子 //求从0到n的骰子掷的次数的期望 // ...
- Spring Boot从入门到实战:整合Web项目常用功能
在Web应用开发过程中,一般都涵盖一些常用功能的实现,如数据库访问.异常处理.消息队列.缓存服务.OSS服务,以及接口日志配置,接口文档生成等.如果每个项目都来一套,则既费力又难以维护.可以通过Spr ...
- caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel
c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...