CDOJ 92 Journey LCA乱搞
原题链接: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乱搞的更多相关文章
- CDOJ 92 Journey(LCA&RMQ)
题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...
- 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】
[题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...
- 洛谷 P1600 天天爱跑步(LCA+乱搞)
传送门 我们把每一条路径拆成$u->lca$和$lca->v$的路径 先考虑$u->lca$,如果这条路径会对路径上的某一个点产生贡献,那么满足$dep[u]-dep[x]=w[x] ...
- cdoj Dividing Numbers 乱搞记忆化搜索
//真tm是乱搞 但是(乱搞的)思想很重要 解:大概就是记忆化搜索,但是原数据范围太大,不可能记下所有的情况的答案,于是我们就在记下小范围内的答案,当dfs落入这个记忆范围后,就不进一步搜索,直接返回 ...
- CF809E Surprise me!(莫比乌斯反演+Dp(乱搞?))
题目大意: 给你一棵树,树上的点编号为\(1-n\).选两个点\(i.j\),能得到的得分是\(\phi(a_i*a_j)*dis(i,j)\),其中\(dis(i,j)\)表示\(a\)到\(b\) ...
- 学渣乱搞系列之Tarjan模板合集
学渣乱搞系列之Tarjan模板合集 by 狂徒归来 一.求强连通子图 #include <iostream> #include <cstdio> #include <cs ...
- [WC2018]通道(乱搞,迭代)
[洛谷题面]https://www.luogu.org/problemnew/show/P4221 这个题以及[CTSC2018 暴力写挂]都有类似的乱搞做法能通过考场数据. 具体搞法就是随一个起点, ...
- URAL 1827 Indigenous Wars(排序、乱搞)
题意:给一个长度为n数组{a[i]}.有m个操作Ti,Si,Li表示找以Ti值结束,以Si值开始,长度为Li的连续子串.找到后,将区间的答案值设为1.一开始答案值全部为0.最后输出n个答案值. 好久没 ...
随机推荐
- 20181205(模块循环导入解决方案,json&pickle模块,time,date,random介绍)
一.补充内容 循环导入 解决方案: 1.将导入的语句挪到后面. 2.将导入语句放入函数,函数在定义阶段不运行 #m1.pyprint('正在导入m1') #②能够正常打印from m2 imp ...
- Kattis - doubleclique (图论)
From : North American Invitational Programming Contest 2018 给你一个图,以及它的补图.如果部分点在原图中是团,并且其他的所有点在补图中也是团 ...
- 动态规划:Codeforces Round #427 (Div. 2) C Star sky
C. Star sky time limit per test2 seconds memory limit per test256 megabytes inputstandard input outp ...
- ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. ...
- SpringMVC之Controller简单使用
//环境 spring-4.3.18/JDK1.8/开发工具/IntelliJ IDEA 2018.2.5 x64 //工程结构图 //web.xml <?xml version="1 ...
- selenium2设置浏览器窗口
1.窗口最大化 //设置窗口最大化driver.manage().window().maximize(); 2.指定设置窗口大小 //指定呀设置窗口的宽度为:800,高度为600Dimension d ...
- 重新造轮子之静态链接1(Static linking)
最近学习计算机病毒学的过程中,又讲到了静态链接的问题,联想到了之前保健哥在信息安全的课堂上向我们展示了一个没有main()函数的C程序到底应该如何编写.个人觉得这个小实验对于加深静态链接的过程的理解也 ...
- python基础学习笔记——异常处理
异常处理流程图 一,异常和错误 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法 ...
- 反转单词顺序 VS 左旋转字符串
题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标垫符号和普通字母一样处理.例如输入字符串“I am a student.”,则输出“student. a am I ...
- day07 类的进阶,socket编程初识
类的静态方法: 正常: 添加静态方法: 就会变成一个函数,不会自动传self 参数,不会调用类的变量和实例的变量 不在需要self 名义上归类管,但是它就是一个单独的函数,不在需要传入self,想怎 ...