QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#511186 | #4815. Flower's Land | peter | WA | 36ms | 944340kb | C++14 | 2.1kb | 2024-08-09 17:14:19 | 2024-08-09 17:14:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=4e4+5;
const int maxk=3e3+5;
int dp[maxn][maxk],f[maxn][maxk];
struct edge{
int to,nxt;
}e[maxn<<1];
int head[maxn],len,n,k,a[maxn],rt,tot,res[maxn];
int sz[maxn],maxx[maxn],dfx[maxn],tim=0;
bool bk[maxn];
inline void init(){
memset(head,-1,sizeof(head));
memset(dp,-0x3f,sizeof(dp));
memset(f,-0x3f,sizeof(f));
memset(res,-0x3f,sizeof(res));
len=0;
maxx[0]=inf;
}
void add(int u,int v){
e[++len].to=v;
e[len].nxt=head[u];
head[u]=len;
}
void dfs(int u,int f){
dfx[++tim]=u;
sz[u]=1;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(v==f||bk[v]) continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
void getrt(int u,int f){
sz[u]=1;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(v==f||bk[v]) continue;
getrt(v,u);
sz[u]+=sz[v];
maxx[u]=max(maxx[u],sz[v]);
}
maxx[u]=max(maxx[u],tot-sz[u]);
if(maxx[rt]>maxx[u]) rt=u;
}
void solve(int u){
bk[u]=1;
tim=0;
dfs(u,0);
dp[1][0]=f[sz[u]+1][0]=0;
for(int i=1;i<sz[u];i++){
for(int j=0;j<k;j++) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+a[dfx[i]]);
if(i!=1){
for(int j=0;j<=k;j++) dp[i+sz[dfx[i]]][j]=max(dp[i+sz[dfx[i]]][j],dp[i][j]);
}
}
for(int i=sz[u];i>=1;i--){
for(int j=1;j<=k;j++) f[i][j]=f[i+1][j-1]+a[dfx[i]];
for(int j=0;j<=k;j++) res[dfx[i]]=max(res[dfx[i]],dp[i][j]+f[i][k-j]);
if(i!=1){
for(int j=0;j<=k;j++) f[i][j]=max(f[i][j],f[i+sz[dfx[i]]][j]);
}
}
f[sz[u]+1][0]=dp[1][0]=-inf;
for(int i=1;i<=sz[u];i++){
for(int j=0;j<=k;j++) dp[i][j]=f[i][j]=-inf;
}
int lst=tot;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(bk[v]) continue;
if(sz[v]>sz[u]) tot=lst-sz[u];
else tot=sz[v];
if(tot<k) continue;
rt=0;
getrt(v,u);
solve(rt);
}
}
int main(){
init();
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
tot=n;
getrt(1,0);
solve(rt);
for(int i=1;i<=n;i++) printf("%d ",res[i]);
puts("");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 20ms
memory: 944340kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 17 17 20
result:
ok 5 number(s): "20 20 17 17 20"
Test #2:
score: 0
Accepted
time: 36ms
memory: 943640kb
input:
7 4 1 3 2 1 7 12 17 4 6 1 4 5 1 2 5 7 6 3 2
output:
31 13 13 31 21 31 31
result:
ok 7 numbers
Test #3:
score: 0
Accepted
time: 19ms
memory: 943344kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: -100
Wrong Answer
time: 24ms
memory: 943328kb
input:
10 3 19 7 25 18 93 97 21 51 60 80 6 7 7 1 1 9 9 10 10 2 2 5 5 3 3 8 8 4
output:
159 180 169 94 180 215 143 169 159 180
result:
wrong answer 6th numbers differ - expected: '137', found: '215'