并查集 - 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 ...
随机推荐
- android 手机UDP 接受不到数据
一.有的手机不能直接接收UDP包,可能是手机厂商在定制Rom的时候把这个功能给关掉了. 1.可先在oncreate()方法里面实例化一个WifiManager.MulticastLock 对象lock ...
- java代码-----实现有键盘获得的字符串存储在文件中,并从文件中读取后显示在屏幕上
总结: 没体会到 package com.a.b; import java.io.*; public class tsetOut { public static void main(String[] ...
- WindowsPhone自定义控件详解(一) - 控件类库分析
转自:http://blog.csdn.net/mr_raptor/article/details/7251942 为了让你的应用程序更有个性,我们通常会在WP7开发过程中会自定义自己风格的控件,自定 ...
- Windows下编译sqlite3
一.下载 sqlite-amalgamation-3240000:sqlite源代码,主要需要头文件sqlite3.h sqlite-dll-win32-x86-3240000.zip:sqlite3 ...
- PubMed
PubMed 是一个提供生物医学方面的论文搜寻以及摘要,并且免费搜寻的数据库.它的数据库来源为MEDLINE.其核心主题为医学,但亦包括其他与医学相关的领域,像是护理学或者其他健康学科. PubMed ...
- 什么是java序列化,如何实现java序列化?
http://veryti.com/question/539 序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间 ...
- 01——微信小程序官方demo讲解——文件结构
1.环境概览 首先环境配置的部分略过,打开小程序开发工具.选择一个空目录,即可开始一个demo项目. 其中新建成功后的目录如图所示: 2.文件结构描述 如图所示,左边是界面展示,右边是目录结构. 目录 ...
- supervisor+uwsgi+django遇到writing to a closed pipe/socket/fd解决
原因: 最近开发的一个项目,由于有个更新job需要消耗的时间非常长,一度以为更新出现了错误. 经过: 于是打开debug模式测试, 异常开启,调试发现system返回了 writing to a cl ...
- C 语言 - 分支、跳转和循环语句
if 条件判断语句 if 语句结构 格式: if (表达式) { 语句; } 如果表达式成立,就执行大括号中的语句:否则跳过该 if 语句 #include <stdio.h> int m ...
- 根据不同分辨率载入相应CSS样式表
index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...