QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416352#5418. Color the TreeocharinRE 0ms8268kbC++231.6kb2024-05-21 19:37:062024-05-21 19:37:09

Judging History

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

  • [2024-05-21 19:37:09]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:8268kb
  • [2024-05-21 19:37:06]
  • 提交

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();
        r.assign(n,vector<int>(__lg(n),0ll));
        for(int i=0;i<n;++i) r[i][0]=a[i];
        for(int i=1;(1<<i)<=n;++i){
            for(int j=0;j+(1<<i)<=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);
    for(int i=0;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;
        --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[0];
        for(int i=dep[u]+1;i<=x+1;++i) f[u][i]=min(f[u][i],st.querymin(i-dep[u],i));
    };
    dfs(dfs,0,-1);
    int res=0;
    for(auto [x,y]:f[0]) 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: 100
Accepted
time: 0ms
memory: 8268kb

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:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: -100
Runtime Error

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:


result: