QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416388#5418. Color the TreeocharinRE 0ms0kbC++231.6kb2024-05-21 19:48:122024-05-21 19:48:13

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 19:48:13]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-05-21 19:48:12]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;

struct ST{
    vector<vector<int>>r;
    void build(vector<int>a){
        int n=a.size()-1;
        r.assign(n+1,vector<int>(__lg(n)+1,0ll));
        for(int i=1;i<=n;++i) r[i][0]=a[i];
        for(int i=1;i<=__lg(n);++i){
            for(int j=0;j+(1<<i)-1<=n;++j){
                r[j][i]=min(r[j][i-1],r[j+(1<<i-1)][i-1]);
            }
        }
    }
    int querymin(int x,int y){
        int t=__lg(y-x+1);
        return min(r[x][t],r[y-(1<<t)+1][t]);
    }
}st;
map<int,int>f[100008];

void solve(){
    int n;
    cin>>n;
    vector<int>a(n+1);
    for(int i=1;i<=n;++i) cin>>a[i];
    st.build(a);
    vector<vector<int>>e(n);
    for(int i=1;i<n;++i){
        int u,v;
        cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    vector<int>dep(n);
    auto dfs=[&](auto dfs,int u,int fa)->void{
        f[u].clear();
        for(auto v:e[u]){
            if(v==fa) continue;
            dep[v]=dep[u]+1;
            dfs(dfs,v,u);
            if(f[u].size()<f[v].size()) swap(f[u],f[v]);
            for(auto [x,y]:f[v]) f[u][x]+=y;
        }
        int x=dep[u];
        for(auto v:e[u]){
            if(v==fa) continue;
            if(f[v].size()) x=max(x,(*f[v].rbegin()).first);
        }
        f[u][dep[u]]=a[1];
        for(int i=dep[u]+1;i<=x+1;++i) f[u][i]=min(f[u][i],st.querymin(i-dep[u]+1,i+1));
    };
    dfs(dfs,1,0);
    int res=0;
    for(auto [x,y]:f[1]) res+=y;
    cout<<res<<"\n";
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:


result: