QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72651 | #4815. Flower's Land | xuqin | Compile Error | / | / | C++14 | 2.8kb | 2023-01-17 18:31:19 | 2023-01-17 18:31:21 |
Judging History
你现在查看的是最新测评结果
- [2023-08-10 23:21:45]
- System Update: QOJ starts to keep a history of the judgings of all the submissions.
- [2023-01-17 18:31:21]
- 评测
- 测评结果:Compile Error
- 用时:0ms
- 内存:0kb
- [2023-01-17 18:31:19]
- 提交
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
using namespace std;
const int maxn=4020, maxk=320;
struct pj{int y, nxt;} G[maxn<<1];
int len, la[maxn], a[maxn], n, k, sz[maxn], son[maxn];
long long f[maxn][maxk], g[maxn][maxk], tmp[maxk], h[maxk], u[maxn][maxk];
void add_e(int x, int y) {
G[++len].y=y; G[len].nxt=la[x]; la[x]=len;
}
void dfs(int x, int F) {
sz[x]=1;
for(int i=la[x]; i; i=G[i].nxt) {
int y=G[i].y;
if(y!=F) dfs(y, x), sz[x]+=sz[y];
}
tmp[0]=0;
int pre=0;
for(int i=la[x]; i; i=G[i].nxt) {
int y=G[i].y;
if(y==F) continue;
for(int j=0; j<=pre+sz[y]&&j<k; ++j) h[j]=-1e18;
for(int j=0; j<=pre&&j<k; ++j)
for(int l=0; l<=sz[y]&&l+j<k; ++l) h[j+l]=max(h[j+l], tmp[j]+f[y][l]);
for(int j=0; j<=pre+sz[y]&&j<k; ++j) tmp[j]=h[j];
pre+=sz[y];
}
for(int i=0; i<=pre&&i<; ++i) assert(tmp[i]>=0);
for(int i=1; i<=k&&i<=sz[x]; ++i) f[x][i]=tmp[i-1]+a[x]; f[x][0]=0;
}
void dfs1(int x, int F) {
int cnt=0, sum=n-sz[x];
for(int i=0; i<k-1&&i<=sum; ++i) u[0][i]=g[x][i+1]-a[x];
for(int i=la[x]; i; i=G[i].nxt) {
int y=G[i].y;
if(y==F) continue;
son[++cnt]=y;
for(int j=0; j<k-1&&j<=sum; ++j)
for(int l=0; l<=sz[y]&&l+j<k-1; ++l) u[y][l+j]=max(u[y][l+j], u[son[cnt-1]][j]+f[y][l]);
sum+=sz[y];
}
tmp[0]=0;
int pre=0;
for(int i=cnt; i; --i) {
// for(int j=0; j<=pre; ++j) printf("%lld ", tmp[j]); puts("");
int y=son[i]; sum-=sz[y];
// printf("sum=%d pre=%d\n", sum, pre);
for(int j=0; j<k-1&&j<=sum; ++j)
for(int l=0; l<=pre&&l+j<k-1; ++l)
g[y][j+l+2]=max(g[y][l+j+2], a[x]+a[y]+u[son[i-1]][j]+tmp[l]);
g[y][1]=a[y]; g[y][0]=0;
// for(int j=0; j<=sum+pre&&j<k-1; ++j) assert(g[y][j]>=0);
if(i==cnt) {for(int j=1; j<k-1&&j<=sz[y]; ++j) tmp[j]=f[y][j];}
else {
for(int j=0; j<=pre+sz[y]&&j<k-1; ++j) h[j]=-1e18;
for(int j=0; j<=pre&&j<k-1; ++j)
for(int l=0; l<=sz[y]&&l+j<k-1; ++l)
h[l+j]=max(h[l+j], tmp[j]+f[y][l]);
for(int j=0; j<=pre+sz[y]&&j<k-1; ++j) tmp[j]=h[j];
}
pre+=sz[y];
}
for(int i=la[x]; i; i=G[i].nxt) {
int y=G[i].y;
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);
memset(f, 0xcc, sizeof f);
memset(g, 0xcc, sizeof g);
memset(u, 0xcc, sizeof u);
dfs(1, 0);
g[1][1]=a[1]; g[1][0]=0;
dfs1(1, 0);
// for(int i=1; i<=n; ++i)
// for(int j=1; j<=k; ++j) printf("[%d][%d]:%lld %lld\n", i, j, u[i][j], g[i][j]);
for(int i=1; i<=n; ++i) {
long long ans=-1e18;
for(int j=1; j<=k; ++j) ans=max(ans, f[i][j]+g[i][k+1-j]-a[i]);
printf("%lld ", 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
answer.code: In function ‘void dfs(int, int)’: answer.code:34:32: error: expected primary-expression before ‘;’ token 34 | for(int i=0; i<=pre&&i<; ++i) assert(tmp[i]>=0); | ^ answer.code: In function ‘int main()’: answer.code:77:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 77 | scanf("%d%d", &n, &k); | ~~~~~^~~~~~~~~~~~~~~~ answer.code:78:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 78 | for(int i=1; i<=n; ++i) scanf("%d", a+i); | ~~~~~^~~~~~~~~~~ answer.code:80:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 80 | scanf("%d%d", &x, &y), add_e(x, y), add_e(y, x); | ~~~~~^~~~~~~~~~~~~~~~