QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#106605#4815. Flower's LandKostlinWA 5ms18084kbC++143.0kb2023-05-18 10:40:482023-05-18 10:40:52

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-18 10:40:52]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:18084kb
  • [2023-05-18 10:40:48]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_map>
using namespace std;
#define fi first
#define sc second
#define mkp make_pair
#define pii pair<int,int>
typedef long long ll;
const int N=40005,K=3005,oo=1e9,bas=131,mod=1e9+9;
inline int read() {
    int x=0,flag=0;char ch=getchar();
    while(ch<'0'||ch>'9') {flag|=(ch=='-');ch=getchar();}
    while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return flag?-x:x;
}
inline int mx(int x,int y) {return x>y?x:y;}
inline int mn(int x,int y) {return x<y?x:y;}
inline void swp(int &x,int &y) {x^=y^=x^=y;}
inline int as(int x) {return x>0?x:-x;}
inline void ckmax(int&x,int y) {x=mx(x,y);}

int n,k,a[N],hd[N],num;
struct node {
    int nxt,to;
}e[N<<1];
#define v e[i].to
inline void adde(int x,int y) {
    e[++num]=(node){hd[x],y};
    hd[x]=num;
}
int tot,rt,maxp[N],sz[N];bool vis[N];
void getrt(int x,int fa) {
    sz[x]=1; maxp[x]=0;
    for(int i=hd[x];i;i=e[i].nxt) {
        if(v==fa||vis[v]) continue;
        getrt(v,x);
        sz[x]+=sz[v];
        maxp[x]=mx(maxp[x],sz[v]);
    }
    maxp[x]=mx(maxp[x],tot-sz[x]);
    if(maxp[x]<maxp[rt]) rt=x;
}
int dfn[N],rev[N],ans[N],to[N],To[N],f[N][K],g[N][K],g_[N][K];
vector<int> T[N],G[N];
void Dfs(int x,int fa) {
    sz[x]=1; dfn[x]=++dfn[0]; rev[dfn[0]]=x;
    for(int i=hd[x];i;i=e[i].nxt) {
        if(v==fa||vis[v]) continue;
        Dfs(v,x);
        sz[x]+=sz[v];
    }
    to[dfn[x]]=dfn[x]+1; To[dfn[x]]=dfn[x]+sz[x];
    T[to[dfn[x]]].push_back(dfn[x]);
    G[To[dfn[x]]].push_back(dfn[x]);
}
inline void work(int x) {
    dfn[0]=0;
    Dfs(x,0);
    if(sz[x]<k) return ;
    for(int i=1;i<=sz[x];++i)
        for(int j=0;j<=k;++j)
            f[i][j]=-oo,g[i][j]=g_[i][j]=0;
    f[1][0]=0;
    for(int i=1;i<=sz[x];++i) {
        for(int j=0;j<k;++j)
            ckmax(f[to[i]][j+1],f[i][j]+a[rev[i]]),
            ckmax(f[To[i]][j],f[i][j]);
    }
    g[sz[x]][1]=g_[sz[x]][1]=a[rev[sz[x]]];
    for(int i=sz[x];i>=1;--i) {
        for(int j=0;j<k;++j) {
            for(int&V:T[i])
                ckmax(g[V][j+1],g[i][j]+a[rev[V]]),
                ckmax(g_[V][j+1],g[i][j]+a[rev[V]]);
            for(int&V:G[i])
                ckmax(g[V][j],g[i][j]);
        }
    }
    for(int i=1;i<=sz[x];++i)
        for(int j=0;j<k;++j)
            ckmax(ans[rev[i]],f[i][j]+g_[i][k-j]);
    for(int i=0;i<=sz[x]+1;++i) T[i].clear(),G[i].clear();
}
inline void solve(int x) {
    work(x); vis[x]=1;
    for(int i=hd[x];i;i=e[i].nxt) {
        if(vis[v]) continue;
        tot=sz[v]; maxp[rt=0]=oo;
        getrt(v,0); getrt(rt,0);
        solve(rt);
    }
}
int main() {
    n=read();k=read();
    for(int i=1;i<=n;++i) a[i]=read();
    for(int i=1,x,y;i<n;++i) {
        x=read();y=read();
        adde(x,y);adde(y,x);
    }
    tot=n; maxp[rt=0]=oo;
    getrt(1,0);
    solve(rt);
    for(int i=1;i<=n;++i) printf("%d ",ans[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 15452kb

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

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: 5ms
memory: 12628kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: 0
Accepted
time: 1ms
memory: 15460kb

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

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

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: 0
Accepted
time: 5ms
memory: 15544kb

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:

ok 100 numbers

Test #8:

score: -100
Wrong Answer
time: 5ms
memory: 18084kb

input:

200 30
332408 397037 98648 388661 351913 146463 354518 148254 131018 214842 465228 340704 397726 443065 248835 223274 239234 260094 420186 444939 5604 433696 253672 122804 20775 178018 260524 40342 259288 399226 146027 273111 141307 489658 150962 170539 339581 10624 479316 460184 467714 261732 46024...

output:

8467030 8324860 9617392 8550455 8359951 9172656 9584815 9799514 10012956 8550455 10066359 8550455 8912805 9433606 9702095 8825653 9320321 9255088 9726253 8759993 8467030 8825653 8839704 10066359 8986218 10066359 8455940 9639651 9859760 9617392 8868772 8514159 9426790 9859760 8662028 8986218 8324860 ...

result:

wrong answer 6th numbers differ - expected: '8715141', found: '9172656'