QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133239 | #4815. Flower's Land | yfq3 | WA | 2ms | 12472kb | C++14 | 2.5kb | 2023-08-01 19:33:11 | 2023-08-01 19:33:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ll=long long;
using ull=unsigned long long;
using lll=__int128;
using db=double;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vl=vector<ll>;
using vii=vector<pii>;
using vll=vector<pll>;
mt19937 rnd(random_device{}());
mt19937_64 rnd64(random_device{}());
const int N=40005;
int n,K;
int a[N];
vi e[N];
int sz[N],mxs[N],rt,tot;
bool vis[N];
int dfn[N],dfn2[N],p[N],p2[N],tmr,tmr2;
void getrt(int u,int fa)
{
sz[u]=1,mxs[u]=0;
for (int v:e[u])
if (!vis[v]&&v!=fa)
{
getrt(v,u),sz[u]+=sz[v];
if (sz[v]>mxs[u]) mxs[u]=max(mxs[u],sz[v]);
}
mxs[u]=max(mxs[u],tot-sz[u]);
if (mxs[u]<=tot/2) rt=u;
}
void dfs(int u,int fa)
{
sz[u]=1;
dfn[u]=++tmr,p[tmr]=u;
for (int v:e[u])
if (!vis[v]&&v!=fa) dfs(v,u),sz[u]+=sz[v];
dfn2[u]=++tmr2,p2[tmr2]=u;
}
int f[N][3005],g[N][3005],ans[N];
void dfs2(int u,int fa,int c,int w)
{
if (c>K) return;
for (int i=0;i<=K-c;i++) ans[u]=max(ans[u],w+f[dfn[u]+1][i]+g[dfn2[u]-sz[u]][K-c-i]);
for (int v:e[u])
if (v!=fa) dfs2(v,u,c+1,w+a[v]);
}
void solve(int s)
{
// printf("s=%d\n",s);
tmr=tmr2=0;
dfs(s,0);
// printf("tmr %d %d\n",tmr,tmr2);
// printf("p:");
// for (int i=1;i<=tmr;i++) printf("%d ",p[i]);puts("");
// printf("p2:");
// for (int i=1;i<=tmr;i++) printf("%d ",p2[i]);puts("");
if (tmr<K) return;
for (int i=0;i<=K;i++) f[tmr+1][i]=g[0][i]=0;
for (int i=tmr;i>=1;i--)
{
int u=p[i];
for (int j=0;j<=K;j++) f[i][j]=max(j?f[i+1][j-1]+a[u]:0,f[i+sz[u]][j]);
}
for (int i=1;i<=tmr;i++)
{
int u=p2[i];
for (int j=0;j<=K;j++) g[i][j]=max(j?g[i-1][j-1]+a[u]:0,g[i-sz[u]][j]);
}
dfs2(s,0,1,a[s]);
// puts("F:");
// for (int i=1;i<=tmr;i++)
// {
// for (int j=0;j<=K;j++) printf("%d ",f[i][j]);
// puts("");
// }
// puts("G:");
// for (int i=1;i<=tmr;i++)
// {
// for (int j=0;j<=K;j++) printf("%d ",g[i][j]);
// puts("");
// }
// for (int i=1;i<=n;i++) printf("%d ",ans[i]);puts("");
vis[s]=1;
for (int v:e[s])
if (!vis[v]) tot=sz[v],getrt(v,s),solve(rt);
}
int main()
{
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);
e[u].pb(v),e[v].pb(u);
}
tot=n,getrt(1,0),solve(rt);
for (int i=1;i<=n;i++) printf("%d ",ans[i]);
}
/*
5 3
6 10 4 3 4
3 4
4 2
2 5
5 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8772kb
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: 1ms
memory: 10876kb
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: 0ms
memory: 8500kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 2ms
memory: 8820kb
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 137 137 169 159 180
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 8848kb
input:
20 3 932 609 248 720 831 253 418 482 1000 542 436 304 217 163 872 380 704 845 497 610 17 12 1 17 15 17 13 17 2 15 16 2 18 16 8 16 4 16 19 4 6 4 20 19 10 19 9 10 5 10 7 9 3 9 14 5 11 7
output:
2508 2185 1790 1945 2373 1470 1960 1707 2373 2373 1854 1940 1853 1536 2508 1945 2508 1945 2039 1827
result:
ok 20 numbers
Test #6:
score: 0
Accepted
time: 2ms
memory: 9072kb
input:
40 5 1105 1687 737 6321 7793 7325 3443 2983 6912 6304 4211 5325 7774 7857 5121 8331 9317 1042 8291 9698 7373 440 9788 7938 7191 5563 4554 596 9733 4920 5398 3642 844 5733 4048 4417 8279 3054 4596 3153 12 17 12 36 12 15 12 13 12 2 12 30 12 18 12 33 12 4 12 39 12 25 12 20 12 10 12 9 12 23 12 29 12 3 1...
output:
35649 36231 35281 40865 42337 41869 37987 37527 41456 40848 38755 43861 42318 42401 39665 42875 43861 35586 42835 43861 41917 34984 43861 42482 41735 40107 39098 35140 43861 39464 39942 38186 35388 40277 38592 38961 42823 37598 39140 37697
result:
ok 40 numbers
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 12472kb
input:
100 10 11845 7520 37311 70194 67214 68176 40075 13721 13118 2555 27023 65012 36716 47598 62807 83049 95169 73454 955 72471 72461 38753 7766 53638 20670 21008 37771 97099 75063 80585 66232 33603 92301 21230 20888 96576 51530 90712 95603 93535 59988 78079 96958 42006 35041 22283 35258 7871 45967 7101 ...
output:
777955 640628 803421 836304 843042 834286 806185 779831 579059 768665 688155 851049 812544 813708 791287 836304 836304 849282 767065 848299 848289 804863 773876 829466 485824 579059 803881 614459 579059 627078 585962 735460 564264 614459 688155 836304 827358 836304 851049 792569 616369 851049 792569...
result:
wrong answer 55th numbers differ - expected: '486384', found: '493273'