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 ...
随机推荐
- DailyMasalaCMS升级记录
手头上是一个比较老的工程,Jdk1.7 + Tomcat7.0 + Spring 3.x + Hibernate 3.x + Elasticseach 2.x 最近Elasticsearch升级,ja ...
- tomcat下配置https方式
[本地HTTPS]①.<Connector SSLEnabled="true" clientAuth="false" keystoreFile=" ...
- Azkban上传文件报错installation Failed.Error chunking
azkaban 上传文件报错Caused by: java.sql.SQLException: The size of BLOB/TEXT data inserted in one transacti ...
- windows中静态库lib和动态dll的区别及使用方法
1. 静态库lib和动态dll的区别 1.1 项目类型 VS在建Win32项目时,有以下选项: windows应用程序控制台应用程序DLL静态库最后两个类型:DLL和静态库,这两种项目类型是不可以单独 ...
- zookeeper客户端 和服务器连接时版本问题
在使用kafka 和zookeeper 实现实时分析程序时,由于zookeeper部署版本和分析程序导入jar包的版本不一致,导致了当实时分析程序从远程服务器连接kafka集群的zookeeper时报 ...
- js 展开&收缩 二种
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JAVA Eclipse开发Android如何让超出界面的部分自动显示滚动条
在原有布局的最外围添加一层ScrollView,注意原有布局的声明也要删了 <ScrollView xmlns:android="http://schemas.android.com/ ...
- linux设备驱动程序之并发和竞态(二)
事实上这blog都是阅读ldd3时的一些总结,巩固自己的学习.也方便后期的使用.大家也能够直接阅读ldd3原文. 锁陷阱 所谓的锁陷阱就是防止死锁. 不明白的规则: ...
- 学习使用用Eclipse编写java程序
本文讲解了在Eclipse中完成一个HelloWorld程序的编写过程. 刚刚学习java的同学们可能用 记事本编写java源代码,在命令提示符中完成java程序的编译和运行过程.这样的方法对于学习j ...
- Lua学习三----------Lua数据类型
© 版权声明:本文为博主原创文章,转载请注明出处 Lua数据类型 - Lua是动态类型语言,不需要为变量定义类型,只需要为变量赋值 - Lua有8中基本数据类型:nil.boolean.number. ...