并查集(路径更新) LA 3027 Corporative Network
题意:训练指南P192
分析:主要就是一个在路径压缩的过程中,更新点i到根的距离
#include <bits/stdc++.h>
using namespace std; const int N = 2e4 + 5;
struct DSU {
int rt[N], d[N];
void init(void) {
memset (rt, -1, sizeof (rt));
memset (d, 0, sizeof (d));
}
int Find(int x) {
if (rt[x] != -1) {
int root = Find (rt[x]); //先找到根
d[x] += d[rt[x]]; //回溯的过程把距离更新
return rt[x] = root; //路径压缩
}
else return x;
}
void Union(int x, int y) {
if (x == y) {
d[x] = 0; return ;
}
else {
rt[x] = y;
d[x] = abs (x - y) % 1000;
}
}
bool same(int x, int y) {
return Find (x) == Find (y);
}
}dsu; int main(void) {
int T; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
char str[3]; dsu.init ();
int x, y;
while (scanf ("%s", str) == 1) {
if (str[0] == 'O') break;
if (str[0] == 'E') {
scanf ("%d", &x);
dsu.Find (x);
printf ("%d\n", dsu.d[x]);
}
else {
scanf ("%d%d", &x, &y);
dsu.Union (x, y); //初始就是到fa的距离
}
}
} return 0;
}
并查集(路径更新) LA 3027 Corporative Network的更多相关文章
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- 【LA 3027 Corporative Network】
·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种: ...
- LA 3027 Corporative Network
这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...
- 并查集+路径压缩(poj1988)
http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submiss ...
- hdu 1558 线段相交+并查集路径压缩
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction
http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 3027 - Corporative Network
3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...
随机推荐
- IIS配置默认文档
我们在配置IIS的默认文档时是在这里配置的,如下图: 但是,有可能我们的根目录下没有这个文件,而且我们网站运行的时候也不想访问根目录下的这个文件,而是要访问其他文件夹下的某一个文件,比如网站运行的时候 ...
- iOS - OC/Swift:验证手机号/固话用正则表达式
/** * 验证手机号是否正确 * @param unknown_type $mobile */ OC: - (BOOL)isMobileNumber:(NSString *)mobileNum { ...
- Lattice Diamond 和 ispLEVER 的不同之处
Lattice Diamond 和 ispLEVER.有一些不同,尤其是如何管理工程的不同,包括以下几点: 1.ispLEVER 有多种工程类型,不同的程序文件类型需要不同的类型的工程:但是Diamo ...
- 并发用户数与 TPS 之间的关系
1. 背景 在做性能测试的时候,很多人都用并发用户数来衡量系统的性能,觉得系统能支撑的并发用户数越多,系统的性能就越好:对TPS不是非常理解,也根本不知道它们之间的关系,因此非常有必要进行解释. 2 ...
- Jmeter 函数
一._csvRead 函数 _cvsRead函数是从外部读取参数,csvRead函数可以从一个文件中读取多个参数. 步骤: 1.先新建一个文件,例如c.txt,里面的数据存放为 web@qq.com, ...
- PHP获取远程图片并调整图像大小(转)
<?php /** * *函数:调整图片尺寸或生成缩略图 *修改:2013-2-15 *返回:True/False *参数: * $Image 需要调整的图片(含路径) * $Dw=450 调整 ...
- Android中Thread和Service的区别zz
1). Thread:Thread 是程序执行的最小单元,它是分配CPU的基本单位.可以用 Thread 来执行一些异步的操作. 2). Service:Service 是android的一种机制,当 ...
- Pyqt QListWidget 展示系统环境变量
今天学习了下Pyqt的 QListWidget 控件 我们先看下这个图片 这张图片就是典型的listWidget效果,我们今天就仿这样布局新建个ListWidget 在网上找了个关于QListWidg ...
- 计算G711语音的打包长度和RTP里timestamp(时间戳)的增长量
转自:http://blog.csdn.net/xujianglun/article/details/48342367 如何计算G711语音等的打包长度和RTP里timestamp的增长量 一般对于不 ...
- linux使用读写锁pthread_rwlock_t
转自:http://blog.csdn.net/onlyou930/article/details/6755593 使用读写锁 配置读写锁的属性之后,即可初始化读写锁.以下函数用于初始化或销毁读写锁. ...