并查集 - 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 ...
随机推荐
- hadoop中的一次集群任务执行超时问题查找过程
问题背景 本次进行一个项目的重构,在某些活动数据量比较大的情况下,会偶尔出现1200s超时的情况,如下: AttemptID:attempt_1410771599055_11709_m_000033_ ...
- css选择器30种
CSS 选择器是一种模式,用于选择需要添加样式的元素.平时使用最多也是最简单的就是 #id..class 和标签选择器,在 CSS 中还有很多更加强大更加灵活的选择方式,尤其是在 CSS3 中,增加了 ...
- Vmware虚拟机linux上网问题
1.虚拟机linux上网问题 1.1 VMware中虚拟机网络的三种设置 第一种:桥接(bridged) 第二种:NAT 第三种:Host only . 这种模式下仅主机可以上网,虚拟机不能上网. 1 ...
- Thread pools & Executors
Thread pools & Executors Run your concurrent code in a performant way All about thread pools # H ...
- Java多线程同步机制(synchronized)
参看:http://enetor.iteye.com/blog/986623
- form中input是类型有哪些?
text:文本框 password:密框码 radio:单选按钮 checkbox:复选框 file:文件选择域 hidden:隐藏域 button:按钮 reset:重置按钮 submit:表单提交 ...
- Sqlmap用法小结
一共有七个等级0.只显示python错误以及严重的信息.1.同时显示基本信息和警告信息.(默认)2.同时显示debug信息.3.同时显示注入的payload.4.同时显示HTTP请求.5.同时显示HT ...
- 移动app非功能测试点收集
非功能测试 移动app测试的另一重要方面是移动app的非功能需求.移动app在推出市场或进行进一步开发前,移动测试员有许多需要测试的问题. 早期开发阶段要进行的第一个测试应该是实用性测试.通常是由al ...
- Hexo+Github/Coding免费搭建个人博客网站
体验更优排版请移步原文:http://blog.kwin.wang/other/hexo-github-build-blog.html 很早之前就想搭建一个属于自己的博客网站,一方面是给自己做笔记,把 ...
- HTML 空白
HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...