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 ...
随机推荐
- java中的json
josn: 一种数据传输格式,与开发语言无关,轻量级 一开始是javaScript的,但是后面比较流传,几乎所有语言都有相应的使用API 数据结构: Object---对象 使用花括号{}包含的键值对 ...
- 别了WindowsXP
生命中有太多的迎来送往,今日全世界都在告别它. 虽然自己已经在很久之前没有用XP系统了.告别它不如在一定意义上告别自己的一段时光... 2001年个人第一台电脑...初次安装XP,两张光盘一张安装盘一 ...
- OO问题
设计一个在线的酒店预订系统,并且可以通过城市搜索出来 解决办法: Main Class: User Room Hotel Booking Adress Enums : 房间的状态和类型 public ...
- URAL - 1243 - Divorce of the Seven Dwarfs (大数取模)
1243. Divorce of the Seven Dwarfs Time limit: 1.0 second Memory limit: 64 MB After the Snow White wi ...
- max带来的冲突
题目要求: /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名:sum123.cpp * 作 者:林海云 * 完毕日期:20 ...
- 在Linux的终端中显示BMPString的内容
在上一篇博文中,介绍了怎样在 Windows 的控制台界面下输出 BMPString 的内容,可是那里的方法在 Linux 下不适用.假设将那里的演示样例代码放到 Linux 下运行.输出的结果为乱码 ...
- 开源 java CMS - FreeCMS2.3会员个人资料
原文地址:http://javaz.cn/site/javaz/site_study/info/2015/28577.html 项目地址:http://www.freeteam.cn/ 个人资料 从 ...
- System Databases in SQL Server
https://docs.microsoft.com/en-us/sql/relational-databases/databases/system-databases SQL Server incl ...
- 日期格式,Popup的使用方法,RenderTransform与LayoutTransform的区别
1.画个笑脸给大家娱乐一下: <Canvas Width="200" Height="180" VerticalAlignment="Cente ...
- Kali linux 2016.2(Rolling)里Metasploit的常用模块
端口扫描 auxiliary/scanner/portscanscanner/portscan/ack ACK防火墙扫描scanner/portscan/ftpbounce FTP跳端口扫描scann ...