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 ...
随机推荐
- C语言基础 (3) C语言介绍
01回顾 02 语言介绍 语言是人和人交流,C语言是人和机器交流. 03_为什么学C语言 04_第一个C代码编译运行 #include <stdio.h> int main() { // ...
- NOIp模拟赛三十一
持续降智 分数:100+0+0=100 C题subtask是假的,根本没有部分分中的情况...还我20分QAQ A:[BZOJ4444]国旗计划 B:[agc006f]blackout C:[arc0 ...
- .Net基础杂记
1.面向对象程序思想 面向对象是程序开发的一种机制,特征为封装.继承.多态.以面向对象方式编写程序时,将复杂的项目抽象为多个对象互相协作的模型,然后编写模型结构,声明或实现类型的成员,即描述对象的特征 ...
- virt-install 创建虚拟机
[root@kvm-server vm]# qemu-img create -f qcow2 centos69b-disk0.qcow2 10G Formatting 'centos69b-disk0 ...
- POJ——T1860 Currency Exchange
http://poj.org/problem?id=1860 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29874 ...
- WinCE的C#中使用StreamReader 来读取TXT文档,读取文本文档。
using System.IO; private void button1_Click(object sender, EventArgs e) { string strFilePath = " ...
- Swift中NSDictionaryOfVariableBindings的替代方案
有日子没写东西了,抽点时间练练笔头子,业精于勤荒于嬉~ 近期从OC转到了Swift2,因为Swift一直没有正经学正经用,所以对这门语言的理解基本算是个球...不得不感慨苹果的动作之快.Swift还没 ...
- python之路-------字符串与正則表達式
1.1.#####去掉字符串中的转义符string.strip() print "hello\tworld\n" >>> word="\thello w ...
- OpenCASCADE License FAQs
OpenCASCADE License FAQs 经常用人问我使用OpenCASCADE开发商业软件是否需要付费,下面从OpenCASCADE的官方网站上截取其回答翻译成中文,官方网址:https:/ ...
- 【大话QT之十六】使用ctkPluginFramework插件系统构建项目实战
"使用ctkPluginFramework插件系统构建项目实战",这篇文章是写博客以来最纠结的一篇文章. 倒不是由于技术都多么困难,而是想去描写叙述一个项目架构採用ctkPlugi ...