POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解。
操作完并查集之后,就是要找和0节点在同一个集合的元素有多少。
注意这个操作,须要先找到0的父母节点。然后查找有多少个节点的额父母节点和0的父母节点同样。
这个时候须要对每一个节点使用find parent操作。由于最后状态的时候,节点的parent不一定是本集合的根节点。
#include <stdio.h> const int MAX_N = 30001;
struct SubSet
{
int p, rank;
}sub[MAX_N]; int N, M; void initSub()
{
for (int i = 0; i < N; i++)
{
sub[i].p = i;
sub[i].rank = 0;
}
} int find(int x)
{
if (x != sub[x].p) sub[x].p = find(sub[x].p);
return sub[x].p;
} void unionTwo(int x, int y)
{
int xroot = find(x);
int yroot = find(y);
if (sub[xroot].rank < sub[yroot].rank) sub[xroot].p = yroot;
else
{
if (sub[xroot].rank == sub[yroot].rank) sub[xroot].rank++;
sub[yroot].p = xroot;
}
} int main()
{
int a, b, k;
while (scanf("%d %d", &N, &M) && (N || M))
{
initSub();
for (int i = 0; i < M; i++)
{
scanf("%d", &k);
if (k > 0) scanf("%d", &a);
for (int j = 1; j < k; j++)
{
scanf("%d", &b);
unionTwo(a, b);
}
}
int sus = 1, p = find(0);
for (int i = 1; i < N; i++)
{
if (find(i) == p) sus++;
}
printf("%d\n", sus);
}
return 0;
}
POJ 1611 The Suspects 并查集 Union Find的更多相关文章
- 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 并查集
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(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 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 ...
随机推荐
- mysql 临时表和内存表
查看内存表的最大值: show variables like '%heap%'; mysql> show variables like '%heap%'; +------------------ ...
- WebKit.NET-0.5简单应用
最近想用c#做个简单的浏览器工具,但是网站一些内容不支持c#内置的WebBowser控件,于是只能改用其他内核浏览器进行开发,搜索到WebKit.NET这个封装好的浏览器引擎,需求的功能也都有,于是用 ...
- Zabbix分布式配置
Zabbix是一个分布式监控系统,它可以以一个中心点.多个分节点的模式运行,使用Proxy能大大的降低Zabbix Server的压力,Zabbix Proxy可以运行在独立的服务器上,安装Zabbi ...
- 紫书 习题 8-21 UVa 1621 (问题分析方法)
知道是构造法但是想了挺久没有什么思路. 然后去找博客竟然只有一篇!!https://blog.csdn.net/no_name233/article/details/51909300 然后博客里面又说 ...
- visual studio 2015下python编程的中文字符串问题
visual studio 2015强大的编程功能,编写起python来也是非常方便的,但其对中文字符的支持不是很好,经常发生莫名其妙的错误,最常见的错误是不报错,也不执行代码. 代码简单如下: x= ...
- UVA 11020 Efficient Solutions+multiset的应用
题目链接:点击进入 首先来讲,非常easy看到我们事实上仅仅要维护优势人群的集合:假设增加一个新的人,我们首先看一下优势人群中是否有人会让这个人失去优势,假设没有,则将这个人插入集合中.但要注意到这个 ...
- hadoop-03-安装java
hadoop-03-安装java 1,su root 2, rpm -qa|grep jdk #查看已经安装的jdk 3,rpm -e --nodeps `rpm -qa|grep jdk ` #删除 ...
- python json及mysql——读取json文件存sql、数据库日期类型转换、终端操纵mysql及python codecs读取大文件问题
preface: 近期帮师兄处理json文件,须要读到数据库里面,以备其兴许从数据库读取数据.数据是关于yelp站点里面的: https://github.com/Yelp/dataset-examp ...
- less10 loop循环
less .loop(@counter) when (@counter > 0) { .loop((@counter - 1)); // 递归调用自身 4 3 2 1 0 width: (10p ...
- 安卓操作sqlite3,增删改查
创建 layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...