并查集 - 1611 The Suspects
题目地址: http://poj.org/problem?id=1611
分析:
- 数据结构
- parent[x] 表示 x 元素的父节点位置.
- rank[x] 记录x的子链的长度, 以便在合并的时候减少链条长度. 查找的时候使用了路劲压缩, 所以两个节点的rank差不会大于1, 所以提高的效率也不是很大, 但还是很有帮助.
- quantity[x] 表示x的子节点的个数(包含自身). 对于根节点来说, 就是这个集合的大小.
- build(n) 由于规模 n 在变化, 所以需要多大的规模就只初始化那一部分即可.
- findx(x) 找到x所在集合的根节点的编号, 并且将路径压缩至1(每个节点直接指向父节点)
- mergexy(x,y) 按照rank大小合并, 并且更新quantity和rank
- 下面的代码中, 如果将 while(m-- && scanf("%d",&k)==) 改为: while(scanf("%d",&k)==1 && m--) 则会出现错误, 因为当m == 0 的时候本来没有group, k应该没有, 但是还是scanf将下一组的数据输入了, 所以会造成TLE.
- 代码如下:
#include <stdio.h> #define MAXNUM 30100 //parent[i]记录i号节点对应的父节点编号. 它们属于同一个集合.
int parent[MAXNUM];
//quantity[i]记录指向i或者i的子节点的节点个数.对于根节点即集合大小(指向自身).
int quantity[MAXNUM];
//rank[i] 记录i的深度, 以便建立一棵平衡的树.
int rank[MAXNUM]; void build(int n){
for(int i=;i<n;i++){
parent[i] = i;
rank[i] = ;
quantity[i] = ;
}
} // 找到x所在的集合的根节点.并压缩路径
int findx(int x){
int r = x;
int t;
while(parent[r] != r){
r = parent[r];
}
//将r的所有间接子节点直接指向r本身, 压缩了路径.
while(r != x && parent[x] != x){
t = parent[x];
parent[x] = r;
x = t;
}
return x;
} // 将x,y所在的集合合并
void mergexy(int x, int y){
int rootx = findx(x);
int rooty = findx(y);
if (rootx == rooty)
return;
// 根据秩的大小合并
if(rank[rootx] > rank[rooty]){
parent[rooty] = rootx;
quantity[rootx] += quantity[rooty];
}else if(rank[rootx] == rank[rooty]){
parent[rootx] = rooty;
quantity[rooty] += quantity[rootx];
rank[rooty]++;
}else{
parent[rootx] = rooty;
quantity[rooty] += quantity[rootx];
}
} int main(){
int n,m;
while(scanf("%d%d",&n,&m) && (n || m)){
//初始化数据结构
build(n);
int k,x,y;
while(m-- && scanf("%d",&k)==){
scanf("%d",&x);
for(int i=;i<k;++i){
scanf("%d",&y);
mergexy(x,y);
}
}
printf("%d\n",quantity[findx()]);
}
return ;
}
-->
并查集 - 1611 The Suspects的更多相关文章
- (并查集)The Suspects --POJ --1611
链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- [并查集] 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 ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 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: 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 ...
随机推荐
- java代码啊==indexOf()方法返回字符第一次出现的位置
package com.s.x; public class Wang { public static void main(String[] args) { if ("woaini" ...
- [转]加密经验集 => C#
下载地址 代码摘自 C#高级编程(第7版) 第579页 不对称加密
- 如果先装framework,后装IIS,想使用ASP.NET
32位系统在Dos下运行以下命令C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i 开始安装 ASP.NET (2.0 ...
- 关于Trunk、Hybrid、Access、Tag、Untag、Pvid的关系
一.相关定义 1.Trunk口 Trunk口上可以同时传送多个VLAN的包,一般用于交换机之间的链接. 2.Hybrid口 Hybrid口上可以同时传送多个VLAN的包,一般用于交换机之间的链接或交 ...
- golang代码覆盖率
写了自动化,就得统计代码覆盖率= = 原链接:https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests 其实看了原链 ...
- CentOS7.6安装Git(IUS方式)
官网下载地址:https://git-scm.com/download/linux 第一步:安装第三方存储库IUS curl https://setup.ius.io | sh 第二步:安装git y ...
- maven中的pom配置文件标签的详细介绍
<span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...
- pycharm git工具与coding.net结合
前提:coding.net中的项目是私密项目 问题描述:在使用pycharm自带的git工具clone(或者push)代码时出现 错误如下:Push failed: Failed with error ...
- Halcon学习之三:有关图像通道的函数(R是三通道,B是1通道,G二通道),排列顺序BGR
黑白摄像机会返回每个像素所对应的能量采用结果,这些结果组成了一幅单通道灰度值图像,而对于RGB彩色摄像机,它将返回每个像素所对应的三个采样结果,也就是一幅三通道图像.下面这些是与图像通道有关的函数: ...
- SDN openflow 学习小得
一.openflow 大概的工作原理 SDN 的一个大概简陋图, 同网段通讯 1.我们传统网络 pc1 10.1.1.1 要找同一子网的 pc2 10.1.1.2 通过广播洪泛.找到pc2,然后转发 ...