QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416381 | #5418. Color the Tree | ocharin | RE | 2ms | 8568kb | C++23 | 1.6kb | 2024-05-21 19:45:19 | 2024-05-21 19:45:21 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct ST{
vector<vector<int>>r;
ST(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]);
}
};
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 st(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);
auto dfs=[&](auto dfs,int u,int fa)->void{
f[u].clear();
dep[u]=dep[fa]+1;
for(auto v:e[u]){
if(v==fa) continue;
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));
};
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: 100
Accepted
time: 2ms
memory: 8568kb
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...