题目地址: 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的更多相关文章

  1. (并查集)The Suspects --POJ --1611

    链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  2. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  3. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  4. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  5. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  6. poj 1611 The Suspects(并查集输出集合个数)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  7. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

  8. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  9. POJ 1611 The Suspects (并查集求数量)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

随机推荐

  1. java代码--------打印三角形

    总结:这里主要是for循环里的j<=i而不死j<=i-1;.还有先打印“*” 再打印空格.换行.理解.请用脑子啊 package com.sads; public class Dds { ...

  2. PHP中exit,exit(0),exit(1),exit('0'),exit('1'),die,return的区别

    die('1')  die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别名.该函数只接受一个参数,可以是一个程序返回的数值或 ...

  3. 用IO字节流复制文件-CopyFileByIo

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  4. 浅谈PHP面向对象编程(六、自动加载及魔术方法)

    6.0 自动加载及魔术方法  6.1 自动加载 在PHP开发过程中,如果希望从外部引入一个class.通常会使用incluae和requre方法把定义这个class的文件包含进来.但是,在大型的开发项 ...

  5. [Cpp primer] Library string Type

    In order to use string type, we need to include the following code #include<string> using std: ...

  6. JasperReport报表导出踩坑实录

    写在最前面 翻了翻博客,因为太忙,已经好久没认真总结过了. 正好趁着今天老婆出门团建的机会,记录下最近这段时间遇到的大坑-JasperReport. 六月份的时候写过一篇利用poi文件导入导出的小De ...

  7. PHP根据问题追踪代码技巧一

    1.问题描述: 2.E:\html\pim\php_aspire-mcloud\module\pim\controller\Configure.class.php public function po ...

  8. Crawlspider的自动爬取

    引子 : 如果想要爬取 糗事百科 的全栈数据的方法 ? 方法一 : 基于scrapy框架中的scrapy的递归爬取进行实现(requests模块递归回调parse方法) . 方法二 : 基于Crawl ...

  9. 「小程序JAVA实战」小程序 loading 提示框与页面跳转(37)

    转自:https://idig8.com/2018/09/02/xiaochengxujavashizhanxiaochengxu-loading-tishikuangyuyemiantiaozhua ...

  10. Consul 简介、安装、常用命令的使用

    1 Consul简介 Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul的方案更"一站式" ...