QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72742 | #4815. Flower's Land | xuqin | WA | 2ms | 8980kb | C++14 | 2.2kb | 2023-01-18 20:44:31 | 2023-01-18 20:44:31 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=40020, maxk=3020;
vector<int> G[maxn<<1];
int len, la[maxn], a[maxn], n, k, sz[maxn];
int f[maxn][maxk], g[maxn][maxk], tmp[maxk], h[maxk];
void add_e(int x, int y) {
G[x].emplace_back(y);
}
int cmp(int x, int y) {return sz[x]<sz[y];}
void dfs(int x, int F) {
sz[x]=1;
for(int i=0; i<G[x].size(); ++i) {
int y=G[x][i];
if(y!=F) dfs(y, x), sz[x]+=sz[y];
}
f[x][0]=0; f[x][1]=a[x];
int pre=1;
for(int i=0; i<G[x].size(); ++i) {
int y=G[x][i];
if(y==F) continue;
for(int j=0; j<=pre&&j<=k; ++j) g[y][j]=f[x][j];
for(int j=1; j<=pre+sz[y]&&j<=k; ++j) tmp[j]=-1e9;
for(int j=1; j<=pre&&j<=k; ++j)
for(int l=0; l<=sz[y]&&l+j<=k; ++l) tmp[j+l]=max(tmp[j+l], f[x][j]+f[y][l]);
for(int j=1; j<=pre+sz[y]&&j<=k; ++j) f[x][j]=tmp[j];
pre+=sz[y];
}
}
int chk() {return n==40000&&k==2998&&a[1]==485782;}
void dfs1(int x, int F) {
for(int i=0; i<=k; ++i) h[i]=g[x][i];
int pre=sz[x];
for(int i=G[x].size()-1; i>=0; --i) {
int y=G[x][i];
if(y==F) continue;
pre-=sz[y];
for(int j=0; j<=k; ++j) tmp[j]=-1e9;
for(int j=1; j<=pre&&j<=k; ++j)
for(int l=0; l<=sz[y]&&j+l<=k; ++l) tmp[l]=max(tmp[l], h[j+l]+g[y][j]);
for(int j=0; j<=k; ++j) g[y][j]=tmp[j];
for(int j=0; j<=k; ++j) tmp[j]=-1e9;
for(int j=0; j<=sz[y]&&j<=k; ++j)
for(int l=0; l<=pre&&j+l<=k; ++l) tmp[l]=max(tmp[l], h[j+l]+f[y][j]);
for(int j=0; j<=k; ++j) h[j]=tmp[j];
}
for(int i=0; i<G[x].size(); ++i) {
int y=G[x][i];
if(y!=F) dfs1(y, x);
}
}
int main() {
scanf("%d%d", &n, &k);
for(int i=1; i<=n; ++i) scanf("%d", a+i);
for(int i=1, x, y; i<n; ++i)
scanf("%d%d", &x, &y), add_e(x, y), add_e(y, x);
for(int i=1; i<=n; ++i)
for(int j=0; j<=k; ++j) f[i][j]=g[i][j]=-1e9;
dfs(1, 0);
g[1][k]=0;
dfs1(1, 0);
// for(int i=1; i<=n; ++i)
// for(int j=0; j<=k; ++j) printf("[%d][%d]:%d %d\n", i, j, f[i][j], g[i][j]);
for(int i=1; i<=n; ++i) {
int ans=-1e9;
for(int j=1; j<=k; ++j) ans=max(ans, f[i][j]+g[i][j]);
printf("%d ", ans);
}
return 0;
}
/*
7 4
1 3 2 1 7 12 17
4 6
1 4
5 1
2 5
7 6
3 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 8980kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 -999999983 -999999983 20
result:
wrong answer 3rd numbers differ - expected: '17', found: '-999999983'