比赛 2023级模拟测试1 评测结果 AAAAATTTTTTT
题目名称 苍天树 最终得分 42
用户昵称 ┭┮﹏┭┮ 运行时间 37.268 s
代码语言 C++ 内存使用 47.70 MiB
提交时间 2023-09-05 19:38:50
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
int n,m,s,ss;
ll su;
ll ans[N],v[N];
struct made{
    int ver,nx,ed;
}e[2*N];
int tot,hd[N];
void add_(int x,int y,int z){
    tot++;
    e[tot].ed = z,e[tot].nx = hd[x],e[tot].ver = y,hd[x] = tot;
}
void sou(int x,int fa){
    if(x == ss){
        v[x] = 1;
        return;
    }
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(y != fa){
            sou(y,x);
            if(v[y] == 1)v[x] = 1;
        }
    }
}
void work(int x,int fa){
    ans[x] = max(ans[x],su);
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver,z = e[i].ed;
        if(v[y] && y != fa){
            su += z;
            work(y,x);
        }
    }
}
int main(){
    freopen("ether_tree.in","r",stdin);
    freopen("ether_tree.out","w",stdout);
    scanf("%d%d%d",&n,&m,&s);
    memset(ans,-1,sizeof(ans));
    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add_(i+1,x,y);
        add_(x,i+1,y);
    } 
    su = 0;
    for(int i = 1;i <= m;i++){
        memset(v,0,sizeof(v));
        int y;
        scanf("%d%d",&ss,&y);
        sou(s,0);
        work(s,0);
        su += y;
        ans[ss] = max(ans[ss],su);
        s = ss;
    }
    for(int i = 1;i <= n;i++)printf("%lld ",ans[i]);
    printf("\n");
      
    return 0;
    
}