LCA UESTC 92 Journey
题意:先给一棵树,然后有一条额外的边,问u走到v从现在最短的路走和原来不加边走的路节省了多少距离
分析:首先跑不加边的树的LCA,这样能求出任意两点的距离,那么现在x和y多连了一条边,如果能节省路程那一定是走了xy这条边,那么暴力枚举组合,比如求u到v,新边xy,ans = min (ans, min (dux + dxy + dyv, duy + dyx + dxv))
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std; typedef long long ll;
const int N = 1e5 + 10;
const int D = 20;
const int INF = 0x3f3f3f3f;
struct Edge {
int v, w, nex;
Edge (int v = 0, int w = 0, int nex = 0) : v (v), w (w), nex (nex) {}
}edge[N<<1];
int head[N], dep[N], rt[D][N];
int d[N];
int n, q, e;
int x, y, cost; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w) {
edge[e] = Edge (v, w, head[u]);
head[u] = e++;
} void DFS(int u, int fa, int deep, int len) {
dep[u] = deep; d[u] = len; rt[0][u] = fa;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v, w = edge[i].w;
if (v == fa) continue;
DFS (v, u, deep + 1, len + w);
}
} int LCA(int u, int v) {
if (dep[u] < dep[v]) swap (u, v);
for (int i=0; i<D; ++i) {
if ((dep[u] - dep[v]) >> i & 1) {
u = rt[i][u];
}
}
if (u == v) return u;
for (int i=D-1; i>=0; --i) {
if (rt[i][u] != rt[i][v]) {
u = rt[i][u];
v = rt[i][v];
}
}
return rt[0][u];
} int solve(int u, int v) {
int lca = LCA (u, v);
int ans = d[u] + d[v] - d[lca] * 2; int lca1 = LCA (u, x);
int dux = d[u] + d[x] - d[lca1] * 2; int lca2 = LCA (u, y);
int duy = d[u] + d[y] - d[lca2] * 2; int lca3 = LCA (v, x);
int dvx = d[v] + d[x] - d[lca3] * 2; int lca4 = LCA (v, y);
int dvy = d[v] + d[y] - d[lca4] * 2; int mn = min (dux + dvy + cost, duy + dvx + cost);
if (mn > ans) return 0;
else return ans - mn;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
init ();
scanf ("%d%d", &n, &q);
for (int u, v, w, i=1; i<n; ++i) {
scanf ("%d%d%d", &u, &v, &w);
add_edge (u, v, w);
add_edge (v, u, w);
}
scanf ("%d%d%d", &x, &y, &cost);
DFS (1, -1, 0, 0);
for (int i=1; i<D; ++i) {
for (int j=1; j<=n; ++j) {
rt[i][j] = rt[i-1][j] == -1 ? -1 : rt[i-1][rt[i-1][j]];
}
}
printf ("Case #%d:\n", ++cas);
for (int u, v, i=1; i<=q; ++i) {
scanf ("%d%d", &u, &v);
printf ("%d\n", solve (u, v));
}
} return 0;
}
LCA UESTC 92 Journey的更多相关文章
- cdoj 92 Journey tarjan/lca 树上点对距离
Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/92 Descri ...
- CDOJ 92 Journey(LCA&RMQ)
题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...
- CDOJ 92 Journey LCA乱搞
原题链接:http://acm.uestc.edu.cn/#/problem/show/92 题意: 给你一棵树,然后在树上连接一条边.现在有若干次询问,每次问你两个点(u,v)之间的距离在加那条边之 ...
- UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)
Description Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, ...
- CDOJ 92 – Journey 【LCA】
[题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...
- POJ 3278:The merchant(LCA&DP)
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6864 Accepted: 2375 Desc ...
- UESTC(LCA应用:求两点之间的距离)
Journey Time Limit: 15000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Bob has ...
- UESTC 912 树上的距离 --LCA+RMQ+树状数组
1.易知,树上两点的距离dis[u][v] = D[u]+D[v]-2*D[lca(u,v)] (D为节点到根节点的距离) 2.某条边<u,v>权值一旦改变,将会影响所有以v为根的子树上的 ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
随机推荐
- dp求顺序hdu1160
题意是仅仅求一次的顺序.先依照速度从大到小排序,速度想等到按体重增序排列. 然后基本就变成了求已定顺序序列的最长递增序列递增,跟那个求一致最大序列和的基本一致. dp[i]里存储的是到当前i最大的递增 ...
- 一个UserState(WCF)的小例子
练习WCF的时候,遇到了一个异步调用(也许是)的问题.后来使用了重载参数UserState试着解决了一下,但不是很清楚这个参数的其他用途.现在贴出这个例子. Service部分: 客户端后台调用部分: ...
- JVM的CPU资源占用过高问题的排查
互联网后端架构 https://mp.weixin.qq.com/s/LiqAy2DikbmZzqogb5XRdA JVM的CPU资源占用过高问题的排查 互联网后端架构 今天 上午线上某应用的一台J ...
- 探索C++的底层机制
探索C++的底层机制 在看这篇文章之前,请你先要明白一点:那就是c++为我们所提供的各种存取控制仅仅是在编译阶段给我们的限制,也就是说是编译器确保了你在完成任务之前的正确行为,如果你的行为不正确,那么 ...
- NBUT 1222 English Game(trie树+DP)
[1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 问题描写叙述 This English game is a simple English words ...
- vue中如何实现后台管理系统的权限控制
vuejs单页应用的权限管理实践 一.前言 在广告机项目中,角色的权限管理是卡了挺久的一个难点.首先我们确定的权限控制分为两大部分,其中根据粒的大小分的更细: 接口访问的权限控制 页面的权限控制 菜单 ...
- oracle重命名数据文件
重命名数据文件 方法1: sql>alter tablespace users offline; sql>host cp /u01/app/oracle/oradata/orcl/us ...
- 书写优雅的shell脚本(插曲)- ed编辑器
今天发现了一个灰常好用的unix/linux文本行编辑器:ed,对于进行面向行的文本编辑工作她真是太棒了,比vi操作简单.方便.功能强大.感兴趣的同仁可以尝试一下!
- codeforces 696B B. Puzzles(树形dp+概率)
题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 23
A题 分析:注意两个点之间的倍数差,若为偶数则为YES,否则为NO #include "iostream" #include "cstdio" #include ...