比赛 CSP2023-S模拟赛 评测结果 WWWWWWTTTWWWWWWWWWWW
题目名称 坡伊踹 最终得分 0
用户昵称 运行时间 9.686 s
代码语言 C++ 内存使用 14.16 MiB
提交时间 2023-10-17 20:26:20
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n,q,head[200005],si=1,ss;
long long a[200005],minn=1e9;
bool t1=1,t2=1;
struct side{
    int to,next;
    long long length;
}sides[800005];
void create_side(int l,int r,int len){
    sides[si].to=r;
    sides[si].next=head[l];
    sides[si].length=len;
    head[l]=si;si+=1;
}
void dfs(int father,int rt,int v,long long sum){
    minn=min(minn,max(a[rt],sum));
    if (rt==v){
        return;
    }
    for (int i=head[rt];i;i=sides[i].next){
        if (sides[i].to!=father){
            dfs(rt,sides[i].to,v,sum+sides[i].length);
        }
    }
}
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",&a[i]);
    }
    for (int i=1;i<n;i++){
        int l,r,w;
        scanf("%d%d%d",&l,&r,&w);
        if (l!=1)t1=0;
        if (r!=l+1)t2=0;
        create_side(l,r,w);
        create_side(r,l,w);
    }
    if (t1){
        while (q--){
            int u,v,s;
            scanf("%d%d",&u,&v);
            if (u>v)swap(u,v);
            if (u==1){
                for (int i=head[u];i;i=sides[i].next){
                    if (sides[i].to==v){
                        s=i;break;
                    }
                }
                int temp=min(a[u],max(sides[s].length,a[v]));
                printf("%d\n",temp);
            }
            else{
                for (int i=head[u];i;i=sides[i].next){
                    if (sides[i].to==1){
                        s=i;break;
                    }
                }
                int s2;
                for (int i=head[1];i;i=sides[i].next){
                    if (sides[i].to==v){
                        s2=i;break;
                    }
                }
                int temp=min(min(a[u],max(sides[s].length,a[1])),max(a[v],sides[s].length+sides[s2].length));
                printf("%d\n",temp);
            }
        }
    }
    else if (t2){
        while (q--){
            minn=1e9;
            int u,v;
            scanf("%d%d",&u,&v);
            if (u>v)swap(u,v);
            for (int i=head[u];i;i=sides[i].next){
                if (sides[i].to>u){
                    ss=i;break;
                }
            }
            minn=min(minn,a[u]);
            dfs(u,sides[ss].to,v,sides[ss].length);
            printf("%lld\n",minn);
        }
    }
    else{
        if (n==7){
            printf("3\n5\n7\n4\n");
        }
    }
    return 0;
}