QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72561#4815. Flower's LandZesty_FoxWA 4ms10164kbC++142.5kb2023-01-16 17:08:132023-01-16 17:08:14

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-16 17:08:14]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:10164kb
  • [2023-01-16 17:08:13]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using i64=long long;
using u64=unsigned long long;
using db=double;
using pii=pair<int,int>;
using vi=vector<int>;

template<typename T>
inline T read(){
    T x=0,f=0;char ch=getchar();
    while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
    while(isdigit(ch)) x=x*10+(ch-'0'),ch=getchar();
    return f?-x:x;
}

#define rdi read<int>
#define rdi64 read<i64>
#define fi first
#define se second
#define pb push_back

const int N=4e4+10,K=3e3+10;

int n,k,a[N];
vi e[N];

int f[N][K],siz[N];
void dfs1(int x,int fa){
    siz[x]=1,f[x][1]=a[x];
    for(auto y:e[x]){
        if(y==fa) continue;
        dfs1(y,x);
        static int tmp[K];
        fill(tmp,tmp+min(siz[x]+siz[y],k)+1,0);
        for(int i=1;i<=min(k,siz[x]);i++)
            for(int j=0;j<=min(k-i,siz[y]);j++)
                tmp[i+j]=max(tmp[i+j],f[x][i]+f[y][j]);
        copy(tmp,tmp+min(siz[x]+siz[y],k)+1,f[x]);
        siz[x]+=siz[y];
    }
}

int g[N][K],res[N];
int h1[N][K],s1[N],s2[N];
void dfs2(int x,int fa){
    for(int i=1;i<=min(k,siz[x]);i++)
        res[x]=max(res[x],f[x][i]+g[x][k-i]);

    static int tmp[K];
    int sz=min(n-siz[x]+1,k);
    tmp[0]=0;
    for(int i=0;i<=min(n-siz[x],k);i++) tmp[i+1]=g[x][i]+a[x];

    for(auto y:e[x]){
        if(y==fa) continue;
        copy(tmp,tmp+min(sz,k)+1,h1[y]),s1[y]=sz;
        fill(tmp,tmp+sz+1,0);
        for(int i=1;i<=sz;i++)
            for(int j=0;j<=min(k-i,siz[y]);j++)
                tmp[i+j]=max(tmp[i+j],h1[y][i]+f[y][j]);
        sz=min(sz+siz[y],k);
    }

    reverse(e[x].begin(),e[x].end());
    fill(tmp,tmp+k+1,0),sz=0;
    for(auto y:e[x]){
        if(y==fa) continue;
        for(int i=1;i<=s1[y];i++)
            for(int j=0;j<=min(k-i,sz);j++)
                g[y][i+j]=max(g[y][i+j],h1[y][i]+tmp[j]);
        static int tmp1[K];
        copy(tmp,tmp+sz+1,tmp1),fill(tmp,tmp+sz+1,0);
        for(int i=0;i<=sz;i++)
            for(int j=0;j<=min(k-i,siz[y]);j++)
                tmp[i+j]=max(tmp[i+j],tmp1[i]+f[y][j]);
        sz=min(sz+siz[y],k);
    }
    for(auto y:e[x])
        if(y!=fa) dfs2(y,x);
}

int main(){
#ifdef LOCAL
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif
    n=rdi(),k=rdi();
    for(int i=1;i<=n;i++) a[i]=rdi();
    for(int i=1;i<n;i++){
        int x=rdi(),y=rdi();
        e[x].pb(y),e[y].pb(x);
    }
    dfs1(1,0),dfs2(1,0);
    for(int i=1;i<=n;i++) cout<<res[i]<<' ';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 6608kb

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: 6444kb

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: 3ms
memory: 6424kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: 0
Accepted
time: 4ms
memory: 9948kb

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: 3ms
memory: 10164kb

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: -100
Wrong Answer
time: 4ms
memory: 10112kb

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 36321 35371 40955 42427 41959 38077 37617 41546 40938 38845 43861 42408 42491 39755 42965 43861 35676 42925 44332 42007 35074 44367 42572 41825 40197 39188 35230 44367 39554 40032 38276 35478 40367 38682 39051 42913 37688 39230 37787 

result:

wrong answer 2nd numbers differ - expected: '36231', found: '36321'