原题链接:http://acm.uestc.edu.cn/#/problem/show/92

题意:

给你一棵树,然后在树上连接一条边。现在有若干次询问,每次问你两个点(u,v)之间的距离在加那条边之后减小了多少。

题解:

对于那条加入的边,只有两种情况,要么走,要么不走。不走的距离就是$dis[u]+dis[v]-2*dis[LCA(u,v)]$,其中$dis$表示点到根节点的距离,LCA表示最近公共祖先。现在考虑走的情况:设加入的那条边是$(a,b)$,边权是c,那么答案显然是:

$$min(DIS(a,u)+DIS(b,v)+c,DIS(a,v)+DIS(b,u)+c)$$

其中DIS表示两点间在树上的最短距离。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define MAX_N 100005
#define MAX_D 22
using namespace std; struct edge {
public:
int to, cost; edge(int t, int c) : to(t), cost(c) { } edge() { }
}; vector<edge> G[MAX_N]; int n,q; int ancestor[MAX_N][MAX_D];
int depth[MAX_N]; int dis[MAX_N]; void init(){
memset(dis,,sizeof(dis));
memset(ancestor,,sizeof(ancestor));
memset(depth,,sizeof(depth));
for(int i=;i<=n;i++)G[i].clear();
} void dfs(int u,int p) {
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
if (v == p)continue;
dis[v]=dis[u]+G[u][i].cost;
depth[v] = depth[u] + ;
ancestor[v][] = u;
dfs(v, u);
}
} void getAncestor() {
for (int j = ; j < MAX_D; j++)
for (int i = ; i <= n; i++)
ancestor[i][j] = ancestor[ancestor[i][j - ]][j - ];
} int LCA(int u,int v) {
if (depth[u] < depth[v])swap(u, v);
for (int i = MAX_D - ; i >= ; i--) {
if (depth[ancestor[u][i]] >= depth[v]) {
u = ancestor[u][i];
if (depth[u] == depth[v])break;
}
}
if (u == v)return u;
for (int i = MAX_D - ; i >= ; i--) {
if (ancestor[u][i] != ancestor[v][i]) {
u = ancestor[u][i];
v = ancestor[v][i];
}
}
return ancestor[u][];
} int getDis(int u,int v) {
int L = LCA(u, v);
return dis[u] + dis[v] - * dis[L];
} int T;
int cas=; int main() {
cin >> T;
while (T--) {
printf("Case #%d:\n", ++cas);
scanf("%d%d", &n, &q);
init();
for (int i = ; i < n - ; i++) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
G[u].push_back(edge(v, c));
G[v].push_back(edge(u, c));
}
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
dfs(, );
getAncestor();
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int tmp, ans;
ans = tmp = getDis(u, v);
ans = min(ans, getDis(u, x) + getDis(y, v) + z);
ans = min(ans, getDis(u, y) + getDis(x, v) + z);
printf("%d\n", tmp - ans);
}
}
return ;
}

CDOJ 92 Journey LCA乱搞的更多相关文章

  1. CDOJ 92 Journey(LCA&RMQ)

    题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...

  2. cdoj 92 Journey tarjan/lca 树上点对距离

    Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/92 Descri ...

  3. CDOJ 92 – Journey 【LCA】

    [题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...

  4. 洛谷 P1600 天天爱跑步(LCA+乱搞)

    传送门 我们把每一条路径拆成$u->lca$和$lca->v$的路径 先考虑$u->lca$,如果这条路径会对路径上的某一个点产生贡献,那么满足$dep[u]-dep[x]=w[x] ...

  5. cdoj Dividing Numbers 乱搞记忆化搜索

    //真tm是乱搞 但是(乱搞的)思想很重要 解:大概就是记忆化搜索,但是原数据范围太大,不可能记下所有的情况的答案,于是我们就在记下小范围内的答案,当dfs落入这个记忆范围后,就不进一步搜索,直接返回 ...

  6. CF809E Surprise me!(莫比乌斯反演+Dp(乱搞?))

    题目大意: 给你一棵树,树上的点编号为\(1-n\).选两个点\(i.j\),能得到的得分是\(\phi(a_i*a_j)*dis(i,j)\),其中\(dis(i,j)\)表示\(a\)到\(b\) ...

  7. 学渣乱搞系列之Tarjan模板合集

    学渣乱搞系列之Tarjan模板合集 by 狂徒归来 一.求强连通子图 #include <iostream> #include <cstdio> #include <cs ...

  8. [WC2018]通道(乱搞,迭代)

    [洛谷题面]https://www.luogu.org/problemnew/show/P4221 这个题以及[CTSC2018 暴力写挂]都有类似的乱搞做法能通过考场数据. 具体搞法就是随一个起点, ...

  9. URAL 1827 Indigenous Wars(排序、乱搞)

    题意:给一个长度为n数组{a[i]}.有m个操作Ti,Si,Li表示找以Ti值结束,以Si值开始,长度为Li的连续子串.找到后,将区间的答案值设为1.一开始答案值全部为0.最后输出n个答案值. 好久没 ...

随机推荐

  1. python 项目中包中__init__.py文件的作用

    开发python项目时,我遇到了一个这样的现象,当我新建一个pythonpackage时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很不了解这个空文件的作用是什么, ...

  2. leetcode-8-pointer

    void deleteNode(ListNode* node) { *node = *node->next; }

  3. ogre3D学习基础6---场景管理器的使用

    场景管理器的使用 最常使用的坐标系统空间(同时也是Ogre程序所能提供的)即是世界空间(World).父节点空间(Parent)以及本地空间(Local). 1.世界空间 就是物体所存在的地方,当我们 ...

  4. Html开发小结

    html部分 1.html标签 标签不区分大小写. 如:<!doctype html>与<!DOCTYPE html > <div></div>与< ...

  5. day01_07.逻辑与字符串运算符

    &&(并且)====>发现&符号总是打错,记忆口令:&7(暗器),在数字7上面,在python中是and ||(或者)====>在python中是or . ...

  6. python学习-- 数据库迁移 python manage.py makemigrations 和 python manage.py migrate

    python manage.py makemigrations 和 python manage.py migrate

  7. Leetcode 421.数组中两数的最大异或值

    数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...

  8. 浅析 Node.js 的 vm 模块以及运行不信任代码

    在一些系统中,我们希望给用户提供插入自定义逻辑的能力,除了 RPC 和 REST 之外,运行客户提供的代码也是比较常用的方法,好处是可以极大地减少在网络上的耗时.JavaScript 是一种非常流行而 ...

  9. HashSet源码分析 jdk1.6

    Set的特点:Set元素无顺序,且元素不可以重复. 1.定义 public class HashSet<E> extends AbstractSet<E> implements ...

  10. iOS--------手势识别的详细使用:拖动、缩放、旋转、点击、手势依赖、自定义手势

    1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...