QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72061#4815. Flower's LandzhouhuanyiWA 3ms9940kbC++112.3kb2023-01-13 21:25:062023-01-13 21:25:06

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-13 21:25:06]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:9940kb
  • [2023-01-13 21:25:06]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#define N 50000
#define M 3000
#define inf 1e9
using namespace std;
int read()
{
    char c=0;
    int sum=0;
    while (c<'0'||c>'9') c=getchar();
    while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
    return sum;
}
int n,k,rt,smz,a[N+1],maxp[N+1],sz[N+1],dfn[N+1],rev[N+1],ans[N+1],dp[N+1][M+1],dp2[N+1][M+1];
bool used[N+1],vis[N+1];
vector<int>E[N+1];
void Adder(int &x,int d)
{
    x=(x<d)?d:x;
    return;
}
void get_rt(int x)
{
    vis[x]=sz[x]=1,maxp[x]=0;
    for (int i=0;i<E[x].size();++i)
	if (!used[E[x][i]]&&!vis[E[x][i]])
	    get_rt(E[x][i]),sz[x]+=sz[E[x][i]],maxp[x]=max(maxp[x],sz[E[x][i]]);
    maxp[x]=max(maxp[x],smz-sz[x]);
    if (maxp[x]<maxp[rt]) rt=x;
    vis[x]=0;
    return;
}
void dfs(int x)
{
    dfn[x]=++dfn[0],rev[dfn[x]]=x,vis[x]=sz[x]=1;
    for (int i=0;i<E[x].size();++i)
	if (!vis[E[x][i]])
	    dfs(E[x][i]),sz[x]+=sz[E[x][i]];
    return;
}
void solve(int x)
{
    int res=0;
    dfn[0]=0,dfs(x),used[x]=1;
    if (dfn[0]<=k)
    {
	for (int i=1;i<=dfn[0];++i) res+=a[rev[i]];
	for (int i=1;i<=dfn[0];++i) ans[rev[i]]=max(ans[rev[i]],res);
	return;
    }
    for (int i=1;i<=dfn[0]+1;++i)
	for (int j=0;j<=k;++j)
	    dp[i][j]=dp2[i][j]=-inf;
    dp[1][0]=dp2[dfn[0]+1][0]=0;
    for (int i=1;i<=dfn[0];++i)
	for (int j=0;j<=min(i,k);++j)
	{
	    if (j+1<=k) Adder(dp[i+1][j+1],dp[i][j]+a[rev[i]]);
	    Adder(dp[i+sz[rev[i]]][j],dp[i][j]);
	}
    for (int i=dfn[0];i>=1;--i)
	for (int j=0;j<=min(dfn[0]+1-i,k);++j)
	{
	    if (j) Adder(dp2[i][j],dp2[i+1][j-1]+a[rev[i]]);
	    Adder(dp2[i][j],dp2[i+sz[rev[i]]][j]);
	}
    for (int i=1;i<=dfn[0];++i)
	for (int j=1;j<=k;++j)
	    Adder(dp[i][j],dp[i][j-1]);
    for (int i=1;i<=dfn[0];++i)
	for (int j=0;j<=k-1;++j)
	    ans[rev[i]]=max(ans[rev[i]],dp[i][j]+dp2[i+1][k-1-j]+a[rev[i]]);
    for (int i=0;i<E[x].size();++i)
	if (!used[E[x][i]])
	    rt=0,smz=sz[E[x][i]],get_rt(E[x][i]),solve(rt);
    return;
}
int main()
{
    int x,y;
    n=read(),k=read(),maxp[0]=inf;
    for (int i=1;i<=n;++i) a[i]=read();
    for (int i=1;i<=n-1;++i) x=read(),y=read(),E[x].push_back(y),E[y].push_back(x);
    rt=0,smz=n,get_rt(1),solve(rt);
    for (int i=1;i<=n;++i) printf("%d ",ans[i]);
    puts("");
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

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

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:

0 180 125 0 180 0 0 0 147 180 

result:

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