QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#416334 | #5418. Color the Tree | fsl123 | RE | 1ms | 4700kb | C++17 | 3.3kb | 2024-05-21 19:27:45 | 2024-05-21 19:27:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+10;
int lg[maxn];
void solve(){
int n;
cin>>n;
vector<ll>a(n+10);
vector<vector<ll>>st(n+10,vector<ll>(20));
for(int i=0;i<n;i++){
cin>>a[i];
st[i][0]=a[i];
}
for(int j=1;j<=19;j++){
for(int i=0;i<n&&i+1<<j-1<n;i++){
st[i][j]=min(st[i][j-1],st[i+1<<(j-1)][j-1]);
}
}
auto mn=[&](int l,int r)->ll{
assert(l>=0&&l<n);
assert(r>=0&&r<n);
assert(l<=r);
int x=lg[r-l+1];
return min(st[l][x],st[r-(1<<x)+1][x]);
};
vector<vector<int>>e(n+10);
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+10),dfn(n+10);
vector<vector<int>>cur(n+10);
vector<vector<int>>fax(n+10,vector<int>(20));
int tot=0;
dep[0]=-1;
auto dfs=[&](auto dfs,int u,int fa)->void{
dfn[u]=++tot;
dep[u]=dep[fa]+1;
fax[u][0]=fa;
for(int i=1;i<=19;i++){
fax[u][i]=fax[fax[u][i-1]][i-1];
}
cur[dep[u]].push_back(u);
for(auto v:e[u]){
if(v==fa) continue;
dfs(dfs,v,u);
}
};
auto lca=[&](int x,int y)->int{
if(dep[x]<dep[y]){
swap(x,y);
}
for(int i=19;i>=0;i--){
if(dep[fax[x][i]]>=dep[y]){
x=fax[x][i];
}
}
if(x==y)return x;
for(int i=19;i>=0;i--){
if(fax[x][i]!=fax[y][i]){
x=fax[x][i],y=fax[y][i];
}
}
return fax[x][0];
};
auto solve=[&](int depp)->int{
if(!cur[depp].size()){
return 0;
}
cur[depp].push_back(1);
sort(cur[depp].begin(),cur[depp].end(),[&](int x,int y){
return dfn[x]<dfn[y];
});
vector<int>now=cur[depp];
for(int i=1;i<cur[depp].size();i++){
int lc=lca(cur[depp][i],cur[depp][i-1]);
//cerr<<cur[depp][i]<<' '<<cur[depp][i-1]<<' '<<depp<<' '<<lc<<'\n';
now.push_back(lc);
}
sort(now.begin(),now.end(),[&](int x,int y){
return dfn[x]<dfn[y];
});
now.erase(unique(now.begin(),now.end()),now.end());
int sz=now.size();
vector<vector<int>>g(sz+10);
vector<ll>dp(sz+10,0);
for(int i=1;i<sz;i++){
int lc=lca(now[i-1],now[i]);
int pos=lower_bound(now.begin(),now.end(),lc)-now.begin();
g[pos].push_back(i);
}
auto dfss=[&](auto dfss,int u,int fa)->void{
dp[u]=a[depp-dep[now[u]]];
ll res=0;
for(auto v:g[u]){
if(v==fa){
continue;
}
dfss(dfss,v,u);
res+=min(mn(depp-dep[now[v]],depp-dep[now[u]]-1),dp[v]);
}
if(res){
dp[u]=min(dp[u],res);
}
};
dfss(dfss,0,-1);
return dp[0];
};
dfs(dfs,1,0);
ll ans=0;
for(int i=0;i<n;++i) ans+=solve(i);
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
for(int i=1;i<=300000;i++){
lg[i]=lg[i-1]+(i==(1<<(lg[i-1]+1)));
}
int T;cin>>T;
while(T--) solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4700kb
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...