QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#235048#4815. Flower's LandwhyWA 0ms1592kbC++172.6kb2023-11-02 11:38:482023-11-02 11:38:49

Judging History

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

  • [2023-11-02 11:38:49]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:1592kb
  • [2023-11-02 11:38:48]
  • 提交

answer

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
using namespace std;

template<typename T>
inline void read(T &x)
{
    T k=1;char ch=getchar();x=0;
    while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x*=k;
}

typedef long long ll;
typedef pair<int,int> pii;
const int N=4e4+86,K=3e3+86,INF=0x7f7f7f7f;

int n,m,ans[N],a[N],f[N][K],g[N][K];
struct edge{int to,nxt;}e[N<<1];
int head[N],tot;
void add(int u,int v){e[++tot]={v,head[u]},head[u]=tot;}

int siz[N],dfn[N],cnt,rt,Min;
bool vis[N];
void dfs(int u,int fa)
{
    siz[u]=1;dfn[++cnt]=u;
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa||vis[v]) continue;
        dfs(v,u);
        siz[u]+=siz[v];
    }
}
void get_act(int u,int fa,int num)
{
    siz[u]=1;
    int res=0;
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa||vis[v]) continue;
        get_act(v,u,num);
        siz[u]+=siz[v];
        res=max(res,siz[v]);
    }
    res=max(res,num-siz[u]);
    if(res<Min) Min=res,rt=u;
}

void calc(int u)
{
    cnt=0;
    dfs(u,0);
    for(int i=1;i<=siz[u]+1;i++)
        for(int j=0;j<=m;j++)
            f[i][j]=g[i][j]=-INF;
    for(int i=0;i<=m;i++)
		f[1][i]=g[siz[u]+1][i]=0;
    for(int i=1;i<=siz[u];i++)
    {
        for(int j=0;j<m;j++)
            f[i+1][j+1]=max(f[i+1][j+1],f[i][j]+a[dfn[i]]);
        if(i^1)
        {
            for(int j=0;j<=m;j++)
                f[i+siz[dfn[i]]][j]=max(f[i+siz[dfn[i]]][j],f[i][j]);
        }
    }
    for(int i=siz[u];i>=1;i--)
    {
        for(int j=1;j<=m;j++)
            g[i][j]=max(g[i][j],g[i+1][j-1]+a[dfn[i]]);
        for(int j=0;j<=m;j++)
            ans[dfn[i]]=max(ans[dfn[i]],g[i][j]+f[i][m-j]);
        if(i^1)
        {
            for(int j=0;j<=m;j++)
                g[i][j]=max(g[i+siz[dfn[i]]][j],g[i][j]);
        }
    }
    vis[u]=true;
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(vis[v]||siz[v]<m) continue;
        Min=INF;
        get_act(v,0,siz[v]);
        calc(rt);
    }
}

void solve()
{
    read(n),read(m);
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=1,u,v;i<=n-1;i++)
        read(u),read(v),add(u,v),add(v,u);
    Min=INF;
    rt=0;
    get_act(1,0,n);
    calc(rt);
    for(int i=1;i<=n;i++) printf("%d ",ans[i]);
}

signed main()
{
    int T=1;
    // read(T);
    while(T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 1544kb

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

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 1592kb

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 16843179 180 16843110 16843169 169 159 180 

result:

wrong answer 4th numbers differ - expected: '94', found: '16843179'