并查集(路径更新) 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 ...
随机推荐
- 解决Idea创建maven-archetype-webapp项目无java目录的问题
一.背景 在适用IDEA创建maven-archetype-webapp项目的时候,创建完成后发现在main文件夹下没有java源文件夹,不少小伙伴也遇到该问题,但不知道怎么解决,下面我就来分享解决步 ...
- mongoose学习笔记1--基础知识1
今天我们将学习Mongoose,什么是Mongoose呢,它于MongoDB又是什么关系呢,它可以用来做什么呢? MongoDB是一个开源的NoSQL数据库,相比MySQL那样的关系型数据库,它更显得 ...
- 3ds max画曲线 设置摄像机的起始位置
参考 http://www.3dmax8.com/3dmax/2013/0916/5661.html 如果想创建曲线段,可以在单击下一个点时按住鼠标不放,继续拖曳,再拖到另一个点上,单击鼠标右键,即可 ...
- ArrayList 和 LinkedList 的区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.2.对于随机访问get和set,ArrayList优于LinkedList,因为LinkedList要移动 ...
- js函数的几个特殊点
在ECMAScript中,Function(函数)类型实际上是对象.每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象 ...
- gitlab 建仓的流程
repository:仓库 Git global setup: git config --global user.name "Administrator" git config - ...
- CLR via C#(13)-浅谈事件
提起事件,我们都不陌生,事件使类之间有了交互的能力.它是建立在委托基础上的.有了前面对委托的了解,相信读起事件来也不会太难了.关于事件,现成的好文章数不胜数,本不打算写了.不过问道有先后,各抒己见,也 ...
- 设计模式学习之观察者模式(Observer,行为型模式)(7)
1.观察者模式又叫做发布-订阅模式. 2.观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 3 ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- CocoaPods安装和使用教程(转摘)
Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...