并查集 - 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 ...
随机推荐
- Nginx 之:nginx.conf结构
nginx.conf文件结构如下: ..... events { ...... } http{ .... server{ .... } server{ listen 192.168.0.12:80; ...
- 【学步者日记】UnityEditor扩展菜单以及ScriptableObject
完整版链接:http://note.youdao.com/noteshare?id=c54f35ca19371886e6a94302387bb6cd 下面是预览的部分,带图的版本请看上面链接. ...
- oracle 存储过程 建表插值等
建表.插值的procedure create or replace procedure CREATE_EMP is v_createsql ); v_insertsql ); begin v_crea ...
- 在Centos中导入sql文件的方法
在Centos中导入sql文件的方法 利用mysql -u root -p进入mysql数据库 对于文件的导入,在Centos下里面的是首先要新建一个和文件相同名字的数据库.例如:mysql>c ...
- 我编辑的JAVA日历程序
class calendar { public static void main(String[]args) { int yearIn ; yearIn = Integer.parseInt(args ...
- Hashtable、HashMap
JDK1.6 API public class Hashtable<K,V>extends Dictionary<K,V>implements Map<K,V>, ...
- python学习笔记(十二):发送邮件
我们在开发程序的时候,有时候需要开发一些自动化的任务,执行完之后,将结果自动的发送一份邮件,python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下: impo ...
- makefile .phony targets
Phony Targets PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字.有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能. 如果编写一个规则,并不产生目标文件,则 ...
- Delegate(代理)异常:该委托必须有一个目标
转自 Delegate(代理)异常:该委托必须有一个目标 在代理调用BeginInvoke(new AsyncCallback(callBack), null);时,会抛这个异常的原因是该代理变量代理 ...
- 一次性show 出所有配置
cisco的全页打印显示配置信息的命令: #terminal length 0 #show run 华为和H3C的全页打印显示配置信息的命令: ]user-interface vty 0 4 ]scr ...