题目传送门

题意:训练指南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. 核心动画基础动画(CABasicAnimation)关键帧动画

    1.在iOS中核心动画分为几类: 基础动画(CABasicAnimation) 关键帧动画(CAKeyframeAnimation) 动画组(CAAnimationGroup) 转场动画(CATran ...

  2. July 8th, Week 28th Friday, 2016

    Care and diligence bring luck. 谨慎和勤奋带来好运气. Just as we have said before, diligence is the mother of g ...

  3. git安装--linux下的安装

    一般linux自带git版本很旧,会有一定问题,可以先卸载系统自带git: yum remove git下载及安装git: wget https://github.com/git/git/archiv ...

  4. 20145206邹京儒《Java程序设计》第8周学习总结

    20145206 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 14.1 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NI ...

  5. hdu 2027统计元音

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2027 思路:主要考察gets()函数用法,能够接受输入的空格,如果用%s或是cin>>st ...

  6. python中random模块使用

  7. MVC - 17.OA项目

          1.分层   2.项目依赖关系 MODEL IDAL -> MODEL DAL -> IDAL,MODEL,EntityFramewrok(注意和MODEL里的版本要一致),S ...

  8. SQL链表查询 数据库为空

    查询出数据为空,解决方案:链表 对应字段长度不一致.

  9. Webstorm & PhpStorm的序列号和证书

    WebStorm注册码 User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3 ...

  10. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...