QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#416473 | #5418. Color the Tree | fsl123 | WA | 1ms | 4688kb | C++17 | 3.3kb | 2024-05-21 21:15:41 | 2024-05-21 21:15:41 |
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>(25));
for(int i=0;i<n;i++){
cin>>a[i];
st[i][0]=a[i];
}
for(int j=1;j<=20;j++){
for(int i=0;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{
if(l<0||(l>=n))return 0;
if(r<0||(r>=n))return 0;
if(l>r)return 0;
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>(25));
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<=20;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=20;i>=0;i--){
if(dep[fax[x][i]]>=dep[y]){
x=fax[x][i];
}
}
if(x==y)return x;
for(int i=20;i>=0;i--){
if(fax[x][i]!=fax[y][i]){
x=fax[x][i],y=fax[y][i];
}
}
return fax[x][0];
};
auto sol=[&](int depp)->ll{
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],now[i-1]);
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+=sol(i);
cerr<<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: 0
Wrong Answer
time: 1ms
memory: 4688kb
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:
wrong answer Answer contains longer sequence [length = 3], but output contains 0 elements