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 ...
随机推荐
- hibernate工作原理及作用
转载自 http://www.cnblogs.com/dashi/p/3597969.html#commentform JAVA Hibernate工作原理及为什么要用 hibernate 简介:hi ...
- HDU 3249 Test for job (有向无环图上的最长路,DP)
解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...
- 检測磁盘驱动的健康程度SMART
在server中,全部组件中一般最easy坏掉的就是磁盘.所以一般採取RAID来保证系统的稳定性,通过冗余磁盘的方式防止磁盘故障. 现代硬件驱动器一般支持SMART(自我监測分析和报告技术),它可以监 ...
- Eclipse中的Web项目自己主动部署到Tomcat
一.原因. 1.写java程序有一段时间了,但非常久没用eclipse了.所以使用eclipse编写的web项目部署到tomcat 的方式也不是非常清楚,以下记录一下将Eclipse 上的web项目自 ...
- Hadoop_stack_cmd
HDFS命令基本格式:Hadoop fs -cmd < args > HDFS命令基本格式:Hadoop fs -cmd < args > ls 命令 hadoop fs -l ...
- DataSnap Mobile Client Tutorial
One of my customers was having some difficulty following the DataSnap tutorial which can be found he ...
- HDU 5178 pairs —— 思维 + 二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others) ...
- Map实现缓存
为什么要使用缓存 缓存最终的目的是为减轻服务端压力,减少网络传输请求 客户端缓存 浏览器访问自带缓存~~ 页面缓存 浏览器缓存 App客户端缓存 IOS 前端开发 底层都有缓存技术的 ( ...
- 一步一步学Silverlight 2系列文章
概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...
- Myeclipse----Hibernate环境搭建
使用myEclipse来生成hibernate 持久化类和映射文件 总体步骤:创建数据库----创建web工程----创建数据视图中的数据库-----导入hibernate框架需要的capabilit ...