• 题意:有\(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)的更多相关文章

  1. [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 ...

  2. ACM International Collegiate Programming Contest World Finals 2014

    ACM International Collegiate Programming Contest World Finals 2014 A - Baggage 题目描述:有\(2n\)个字符摆在编号为\ ...

  3. ACM International Collegiate Programming Contest World Finals 2013

    ACM International Collegiate Programming Contest World Finals 2013 A - Self-Assembly 题目描述:给出\(n\)个正方 ...

  4. 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 ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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\)个白箱,将这 ...

随机推荐

  1. maven生命周期与插件

    目录 Maven生命周期 clean default site 命令与对应周期 插件与绑定 插件目标 插件绑定 内置绑定 自定义绑定 插件配置 本文主要是针对<maven实战>书中关键知识 ...

  2. ptgmk

    num >= 1125899906842624 ? "P" \ : num >= 1099511627776 ? "T" \ : num >= ...

  3. 【EXPDP】Oracle expdp中并行问题

    $ expdp hr/hr tables=test1 dumpfile=test2.dmp directory=pump parallel=4 Export: Release 11.2.0.4.0 - ...

  4. xtrabakcup基本用法 安装、全量备份恢复、增量备份恢复

    xtrabackup备份原理以及工作流程 备份流程日志分析:1.##读取mysql配置文件2.## 扫描innodb日志lsn并复制inndodb系统表空间3.## 缓冲写出到数据文件并锁表4.## ...

  5. linux设备文件

    一.前言 在调用了alloc_chrdev_region函数或register_chrdev_region函数之后可以在/proc/devices中看到该设备的主设备号,比如我注册的hello模块的主 ...

  6. testng学习笔记-- beforeclass和afterclass

    一.定义 类之前和类之后运行的方法 使用场景: 类运行之前是否需要静态方法,变量赋值,写完其他方法都可以用了 二.标签代码 三.运行结果

  7. Hive常用日期格式转换

    固定日期转换成时间戳 select unix_timestamp('2016-08-16','yyyy-MM-dd') --1471276800 select unix_timestamp('2016 ...

  8. (转载)微软数据挖掘算法:Microsoft 线性回归分析算法(11)

    前言 此篇为微软系列挖掘算法的最后一篇了,完整该篇之后,微软在商业智能这块提供的一系列挖掘算法我们就算总结完成了,在此系列中涵盖了微软在商业智能(BI)模块系统所能提供的所有挖掘算法,当然此框架完全可 ...

  9. 数位DP笔记

    数位DP 1.定义: 数位dp是一种计数用的dp,一般就是要统计一个区间[L,R]内满足一些条件数的个数.所谓数位dp,字面意思就是在数位上进行dp: 数位的含义:一个数有个位.十位.百位.千位... ...

  10. 3D运动类申明与实现

    #ifndef PKM3D_H #define PKM3D_H #include"kinematics.h" #include"Inventor/Qt/viewers/S ...