题目传送门

题意:训练指南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的更多相关文章

  1. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  2. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  3. 【LA 3027 Corporative Network】

    ·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:    ...

  4. LA 3027 Corporative Network

    这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...

  5. 并查集+路径压缩(poj1988)

    http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submiss ...

  6. hdu 1558 线段相交+并查集路径压缩

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

  8. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  9. 3027 - Corporative Network

    3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...

随机推荐

  1. php字符串处理函数相关操作

    <?php//获取tech和98426这两个字符串

  2. maven编译时如何忽略单元测试

    共有两种解决办法 1.通过在命令行设置:-Dmaven.test.skip=true 如:mvn clean install tomcat:run -Dmaven.test.skip=true 2.通 ...

  3. android setCompoundDrawables 不显示问题

    在 vh.tvAddr.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_real_state_loc), null, nul ...

  4. 在Linux中安装JDK的步骤

    相信不少学习Java的朋友都在Windows操作系统中安装过JDK,这里就不对JDK做详细的介绍了. 在Windows下安装JDK可参考:JDK的安装和配置 1.下载JDK 我们可以去官网(http: ...

  5. hdu1162(最小生成树 prim or kruscal)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: pri ...

  6. CLR via C#(07)-静态类,分部类

    一.      静态类-Static 静态类是一些不能实例化的类,它的作用是将一些相关的成员组合到一起,像我们常见的Math, Console等.静态类由static关键字标识,静态类成员也只能是st ...

  7. ARPPING

    http://www.tuicool.com/articles/M7B3umj http://lixcto.blog.51cto.com/4834175/1571838/

  8. ActiveMQ的几种消息持久化机制

    为了避免意外宕机以后丢失信息,需要做到重启后可以恢复消息队列,消息系统一般都会采用持久化机制. ActiveMQ的消息持久化机制有JDBC,AMQ,KahaDB和LevelDB,无论使用哪种持久化方式 ...

  9. (四)WebRTC手记之本地音频采集

    转自:http://www.cnblogs.com/fangkm/p/4374668.html 上一篇博文介绍了本地视频采集,这一篇就介绍下音频采集流程,也是先介绍WebRTC原生的音频采集,再介绍C ...

  10. mybatis 中#和$的区别

    #{…}是一个参数标记,将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是1,那么解析成sql时的值为order by " ...