QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104325 | #4815. Flower's Land | 1kri | WA | 2ms | 3704kb | C++14 | 2.2kb | 2023-05-10 09:52:15 | 2023-05-10 09:52:16 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 2000000000
using namespace std;
void updmax(int &x,int y){
if (y>x)x=y;
return;
}
int n,k,a[40005];
int u[100005],v[100005],first[40005],nxt[100005];
int book[40005];
int sum_sz,sz[40005],rt;
void get_rt(int now,int fa){
sz[now]=1;
int mx=0;
for (int i=first[now];i;i=nxt[i])
if (v[i]!=fa&&book[v[i]]==0){
get_rt(v[i],now);
sz[now]+=sz[v[i]];
mx=max(mx,sz[v[i]]);
}
mx=max(mx,sum_sz-sz[now]);
if (2*mx<=sum_sz)rt=now;
return;
}
int f[40005][3005],g[4005][3005];
int ans[40005];
int idx,dfn[40005],nfd[40005],dfo[40005];
int _nxt[40005];
void dfs(int now,int fa){
++idx;
dfn[now]=idx;
nfd[idx]=now;
for (int i=first[now];i;i=nxt[i])
if (v[i]!=fa&&book[v[i]]==0)dfs(v[i],now);
dfo[now]=idx;
return;
}
void calc(int now){
book[now]=1;
idx=0;
dfs(now,0);
if (idx<k)return;
for (int i=1;i<=idx;i++){
for (int j=0;j<=k;j++)
f[i][j]=g[i][j]=-inf;
g[i][0]=0;
g[i][1]=a[nfd[i]];
}
for (int i=1;i<=idx;i++)_nxt[i]=-1;
for (int i=1;i<=idx;i++){
int t=dfo[nfd[i]]+1;
_nxt[i]=t;
}
f[1][0]=0;
for (int i=1;i<=idx;i++)
for (int j=0;j<k;j++){
if (i+1<=idx)updmax(f[i+1][j+1],f[i][j]+a[nfd[i]]);
if (_nxt[i]!=-1){
updmax(f[_nxt[i]][j],f[i][j]);
updmax(f[_nxt[i]][j+1],f[i][j]+a[nfd[i]]);
}
}
for (int i=idx;i>=1;i--)
for (int j=2;j<=k;j++){
if (i+1<=idx)updmax(g[i][j],g[i+1][j-1]+a[nfd[i]]);
if (_nxt[i]!=-1){
updmax(g[i][j],g[_nxt[i]][j]);
updmax(g[i][j],g[_nxt[i]][j-1]+a[nfd[i]]);
}
}
for (int i=1;i<=idx;i++)
for (int j=0;j<k;j++){
int v=a[nfd[i]];
if (i+1<=idx)updmax(v,g[i+1][k-j-1]+a[nfd[i]]);
if (_nxt[i]!=-1)updmax(v,g[_nxt[i]][k-j-1]+a[nfd[i]]);
updmax(ans[nfd[i]],f[i][j]+v);
}
get_rt(now,0);
for (int i=first[now];i;i=nxt[i]){
if (book[v[i]]==1)continue;
sum_sz=sz[v[i]],rt=0;
get_rt(v[i],now);
calc(rt);
}
return;
}
int main(){
cin>>n>>k;
for (int i=1;i<=n;i++)cin>>a[i];
for (int i=1;i<n;i++){
cin>>u[i]>>v[i];
nxt[i]=first[u[i]],first[u[i]]=i;
u[i+n]=v[i],v[i+n]=u[i];
nxt[i+n]=first[u[i+n]],first[u[i+n]]=i+n;
}
calc(1);
for (int i=1;i<=n;i++)printf("%d ",ans[i]);
printf("\n");
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3704kb
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: 2ms
memory: 3648kb
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: 2ms
memory: 3528kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 3620kb
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 211 137 137 169 159 198
result:
wrong answer 5th numbers differ - expected: '180', found: '211'