QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#511212#4815. Flower's Landmsk_samaWA 1ms7980kbC++231.8kb2024-08-09 17:36:192024-08-09 17:36:20

Judging History

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

  • [2024-08-09 17:36:20]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7980kb
  • [2024-08-09 17:36:19]
  • 提交

answer

#include <bits/stdc++.h>
#define MISAKA main
#define ll long long
using namespace std;
inline int read(){
    char ch=getchar();int f=1,x=0;
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
const double eps=1e-9;
const int N=40010,K=3010,inf=1e9,mod=998244353;
int n,k,a[N],f[N][K],g[N][K],ans[N],siz[N],id[N],vis[N],tim=0,mn,rt;
vector<int> G[N];
void getsiz(int u,int fa,int n){
    siz[u]=1;int mx=-1;
    for(int v:G[u])if(v!=fa&&!vis[v])
        getsiz(v,u,n),siz[u]+=siz[v],mx=max(mx,siz[v]);
    mx=max(mx,n-siz[u]);if(mx<mn) mn=mx,rt=u;
}
int findrt(int u,int n){mn=inf;getsiz(u,0,n);return rt;}
void dfs(int u,int fa){id[++tim]=u;for(int v:G[u])if(v!=fa&&!vis[v])dfs(v,u);}
void solve(int u){
    vis[u]=1;tim=0,dfs(u,0);
    for(int i=1;i<=siz[u]+1;i++)
        memset(f[i],-0x3f,sizeof(f[i])),
        memset(g[i],-0x3f,sizeof(g[i]));
    for(int i=0;i<=k;i++) f[1][i]=g[siz[u]+1][i]=0;
    for(int i=1;i<=siz[u];i++){
        for(int j=0;j<k;j++) f[i+1][j+1]=max(f[i+1][j+1],f[i][j]+a[id[i]]);
        if(i^1)for(int j=0;j<k;j++) f[i+siz[id[i]]][j]=max(f[i+siz[id[i]]][j],f[i][j]);
    }
    for(int i=siz[u];i;i--){
        for(int j=0;j<=k;j++) g[i][j]=max(g[i][j],g[i+1][j-1]+a[id[i]]);
        for(int j=0;j<=k;j++) ans[id[i]]=max(ans[id[i]],f[i][j]+g[i][k-j]);
        if(i^1)for(int j=0;j<=k;j++) g[i][j]=max(g[i][j],g[i+siz[id[i]]][j]);
    }
    for(int v:G[u])if(!vis[v]&&siz[v]>=k) solve(findrt(v,siz[v]));
}
signed MISAKA(){
    n=read(),k=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<n;i++){
        int u=read(),v=read();
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int rt=findrt(1,n);siz[rt]=n;solve(rt); 
    for(int i=1;i<=n;i++) printf("%d ",ans[i]);
    return 0;
}

详细

Test #1:

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

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

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: 1ms
memory: 7912kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 6016kb

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:

137 147 118 105 118 137 137 169 147 147 

result:

wrong answer 1st numbers differ - expected: '159', found: '137'