比赛 CSP2023-S模拟赛 评测结果 AAAAAAAAATTTTTTTTTTT
题目名称 坡伊踹 最终得分 45
用户昵称 在大街上倒立游泳 运行时间 33.416 s
代码语言 C++ 内存使用 17.69 MiB
提交时间 2023-10-17 20:58:41
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
struct node{
    int a,f,w,s,c;
    vector<int> ez;
}tree[200005];
vector<pii> g[200005];
bool vis[200005];
int n,q,u,v,d;
void build(int x){
    vis[x]=1;
    tree[x].c=tree[tree[x].f].c+1;
    tree[x].s=tree[tree[x].f].s+tree[x].w;
    for(int i=0;i<g[x].size();i++){
        int xin=g[x][i].first;
        if(!vis[g[x][i].first]){
            tree[g[x][i].first].f=x;
            tree[xin].w=g[x][i].second;
            tree[x].ez.push_back(xin);
            build(xin);
        }
    }
}
int lc(){
    int x1=u,x2=v;
    if(tree[x1].c<tree[x2].c) swap(x1,x2);
    while(tree[x1].c>tree[x2].c) x1=tree[x1].f;
    while(x1!=x2){
        x1=tree[x1].f;
        x2=tree[x2].f;
    }
    return x1;
}
int main(){
    freopen("poitry.in","r",stdin);
    freopen("poitry.out","w",stdout);
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) scanf("%d",&tree[i].a);
    for(int i=1;i<n;i++){
        scanf("%d%d%d",&u,&v,&d);
        g[u].push_back(make_pair(v,d));
        g[v].push_back(make_pair(u,d));
    }
    tree[1].c=1;
    build(1);
    while(q--){
        int ans=2000000000;
        scanf("%d%d",&u,&v);
        int x1=u,x2=v;
        //if(tree[x1].c<tree[x2].c) swap(x1,x2);
        int lca=lc();
        while(tree[x1].c>tree[x2].c){
            ans=min(ans,max(tree[x1].a,tree[u].s-tree[x1].s));
            x1=tree[x1].f;
        }
        while(tree[x2].c>tree[x1].c){
            ans=min(ans,max(tree[x2].a,tree[u].s+tree[x2].s-2*tree[lca].s));
            x2=tree[x2].f;
        }
        while(x1!=x2)
        {
            ans=min(ans,max(tree[x1].a,tree[u].s-tree[x1].s));
            ans=min(ans,max(tree[x2].a,tree[u].s+tree[x2].s-2*tree[lca].s));
            x1=tree[x1].f;
            x2=tree[x2].f;
        }
        ans=min(ans,max(tree[x1].a,tree[u].s-tree[x1].s));
        ans=min(ans,max(tree[x2].a,tree[u].s+tree[x2].s-2*tree[lca].s));
        printf("%d\n",ans);
    }
    return 0;   
}