GYM101810 ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018) M. Greedy Pirate (LCA)
题意:有\(n\)个点,\(n-1\)条边,每条边正向和反向有两个权值,且每条边最多只能走两次,有\(m\)次询问,问你从\(u\)走到\(v\)的最大权值是多少.
题解:可以先在纸上画一画,不难发现,除了从\(u\)走到\(v\)的路径上的反向权值我们取不到,其他所有边的正反权值均能取到,所以答案就是:\(sum-u->v路径的反向权值\),问题也就转换成了求\(v->u\)的权值,那么这里我们就可以用LCA来求了.
首先,令一个点为根节点,然后求出\(v\)到根节点的距离和根节点到\(u\)的距离,再减去根节点到\(LCA(u,v)\)的正反权值,就是\(v->u\)的权值.
这题会卡读入,记得用scanf.
代码:
struct misaka{
int out;
int val1,val2;
}p; int t;
int n,m;
int sum;
vector<misaka> V[N];
int fa[N][30];
int depth[N];
int lg[N];
int dis1[N],dis2[N];
bool st[N]; void dfs(int node){ //求每个节点到根节点的距离.
st[node]=true;
for(auto w:V[node]){
int now=w.out;
int val1=w.val1;
int val2=w.val2;
if(st[now]) continue;
dis1[now]=dis1[node]+val1;
dis2[now]=dis2[node]+val2;
dfs(now);
}
} void presol(int node,int fath){
fa[node][0]=fath;
depth[node]=depth[fath]+1;
for(int i=1;i<=lg[depth[node]]-1;++i){
fa[node][i]=fa[fa[node][i-1]][i-1];
}
for(auto w:V[node]){
if(w.out!=node){
presol(w.out,node);
}
}
} int LCA(int x,int y){
if(depth[x]<depth[y]){
swap(x,y);
}
while(depth[x]>depth[y]){
x=fa[x][lg[depth[x]-depth[y]]-1];
}
if(x==y) return x;
for(int k=lg[depth[x]]=1;k>=0;--k){
if(fa[x][k]!=fa[y][k]){
x=fa[x][k];
y=fa[y][k];
}
}
return fa[x][0];
} int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>t;
while(t--){
cin>>n;
sum=0;
for(int i=1;i<=n-1;++i){
int u,v,val1,val2;
cin>>u>>v>>val1>>val2;
p.out=v,p.val1=val1,p.val2=p.val2;
V[u].pb(p);
p.out=u,p.val1=val2,p.val2=p.val1;
V[v].pb(p);
} for(int i=1;i<=n;++i){
lg[i]=lg[i-1]+(1<<lg[i-1]==i);
} dfs(1);
presol(1,0); cin>>m;
for(int i=1;i<=m;++i){
int u,v;
cin>>u>>v;
cout<<sum-(dis1[u]+dis2[v]-dis1[LCA(u,v)]-dis2[LCA(u,v)])<<endl;
} } return 0;
}
GYM101810 ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018) M. Greedy Pirate (LCA)的更多相关文章
- [ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)]
https://codeforces.com/gym/101810 A. Careful Thief time limit per test 2.5 s memory limit per test 2 ...
- ACM International Collegiate Programming Contest World Finals 2014
ACM International Collegiate Programming Contest World Finals 2014 A - Baggage 题目描述:有\(2n\)个字符摆在编号为\ ...
- ACM International Collegiate Programming Contest World Finals 2013
ACM International Collegiate Programming Contest World Finals 2013 A - Self-Assembly 题目描述:给出\(n\)个正方 ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- Call for Papers IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM)
IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM) 2014 In ...
- IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM) 2014 Industry Track Call for Papers
IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM) 2014 In ...
- 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest
2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...
随机推荐
- 分别使用 Python 和 Math.Net 调用优化算法
1. Rosenbrock 函数 在数学最优化中,Rosenbrock 函数是一个用来测试最优化算法性能的非凸函数,由Howard Harry Rosenbrock 在 1960 年提出 .也称为 R ...
- 【Oracle】dump函数用法
Oracle dump函数的用法 一.函数标准格式: DUMP(expr[,return_fmt[,start_position][,length]]) 基本参数时4个,最少可以填的参数是0个.当完全 ...
- leetcode 1240. 铺瓷砖(回溯,DFS)
题目链接 https://leetcode-cn.com/problems/tiling-a-rectangle-with-the-fewest-squares/ 题意: 用尽可能少的正方形瓷砖来铺地 ...
- Sentry(v20.12.1) K8S 云原生架构探索,1分钟上手 JavaScript 性能监控
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
- C#从入门到放弃治疗一:初探C#世界
C#是一款高级的面向对象语言,运行于.NET framework之上的高级程序设计语言.其语言规范和,语法和java有着惊人的类似之处.所以如果你在学习C#之前有着java的基础,你将快速地入门.当然 ...
- uni-app开发经验分享八: 实现微信APP支付的全过程详解
背景 最近项目使用uni-app实现微信支付,把过程简单记录下,帮助那些刚刚基础uni-app,苦于文档的同学们.整体来说实现过程和非uni-app的实现方式没有太大不同,难点就在于uni-app对于 ...
- centralized collectors 中心化 采集器
Fluent Bit https://fluentbit.io/ FluentBit is an open source specialized data collector. It provides ...
- 《CSP.OI吟》
吟 CSP·OI 这个LCT,我听得很懵逼 在 Splay 里面,好像有重链 不用线段树,Splay 来维护 树的形态有改变,不只是那一条边 所以要把整棵树,重新剖一遍 什么重链 ~ 什么轻边 ~ 什 ...
- 题解 P3833 【[SHOI2012]魔法树】
题目 直通车 很显然这是个树刨的板子,树上链查询和子树查询 注意: 1.这个点的树根为 0 而不是 1 所以注意读图时点标号 +1 就解决了 2.注意数据范围\(2^{32}\) 然后板子就能过了 n ...
- 某商城系统(V1.3-2020-01-10)前台命令执行漏洞
漏洞文件: ./inc/module/upload_img.php 先跟进 del_file 函数: 在 del_file 函数中首先执行了unlink操作,然后接着进行了file_exists 判断 ...