QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#416290 | #5418. Color the Tree | ocharin | RE | 0ms | 3580kb | C++23 | 1.5kb | 2024-05-21 18:53:42 | 2024-05-21 18:53:42 |
Judging History
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;(1<<i)<=n;++i){
for(int j=1;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;
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+1);
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+1),ma(n+1);
vector<map<int,int>>f(n+1);
auto dfs=[&](auto dfs,int u,int fa)->void{
ma[u]=dep[u]=dep[fa]+1;
for(auto v:e[u]){
if(v==fa) continue;
dfs(dfs,v,u);
ma[u]=max(ma[u],ma[v]);
if(f[u].size()<f[v].size()) swap(f[u],f[v]);
for(auto [x,y]:f[v]) f[u][x]+=y;
}
f[u][dep[u]]=a[1];
for(int i=dep[u];i<=ma[u];++i) f[u][i]=min(f[u][i],st.querymin(i-dep[u]+1,i));
};
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();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
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...