QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#275891#4815. Flower's Land_Du_WA 0ms6520kbC++202.7kb2023-12-05 11:05:272023-12-05 11:05:28

Judging History

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

  • [2023-12-05 11:05:28]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:6520kb
  • [2023-12-05 11:05:27]
  • 提交

answer

#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define debug_(x) cerr << #x << " = " << x << ' '
#define debug(x) cerr << #x << " = " << x << '\n'
// #define int long long
#define endl '\n'
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef pair<int, int> PII;
typedef int LL;
const int mod = 998244353;
// const int mod = 1000000000 + 7;
const int N = 2e5 + 10;
const int M = 1e6 + 10;
mt19937_64 mrand(random_device{}());
int n, m, k;
vector<int> adj[40010];
int f[40010][3010],g[40010][3010];
int sz[40010];
int st[40010];
int ans[40010];
int a[40010];
int dfn[40010];
int dfc;
int get_size(int u,int fa){
    if(st[u]) return 0;
    sz[u]=1;
    for(auto v :adj[u]){
        if(v==fa) continue;
        sz[u]+=get_size(v,u);
    }
    return sz[u];
}
int get_wc(int u,int fa,int tot,int &wc){
    if(st[u]) return 0;
    int ms=0;
    int res=1;
    for(auto v : adj[u]){
        if(v==fa) continue;
        int val=get_wc(v,u,tot,wc);
        ms=max(ms,val);
        res+=val;
    }
    ms=max(ms,tot-res);
    if(ms<=tot/2) wc=u;
    return res;
}
void cal(int u){
    if(st[u]) return;
    get_wc(u,-1,get_size(u,-1),u);
    dfc=0;
    auto dfs=[&](auto &&dfs,int u,int fa)->void{
        dfn[++dfc]=u;
        for(auto v : adj[u]){
            if(st[v] || v==fa) continue;
            dfs(dfs,v,u);
        }
    };
    dfs(dfs,1,-1);
    st[u]=1;
    for(int i=1;i<=dfc;i++){
        memset(f[i],-0x3f,sizeof f[i]);
        memset(g[i],-0x3f,sizeof g[i]);
    }
    f[1][0]=0;
    g[dfc][0]=0;
    for(int i=2;i<=dfc;i++)
	{
		for(int j=0;j<k;j++)
			f[i][j+1]=max(f[i][j+1],f[i-1][j]+a[dfn[i-1]]);
        for(int j=0;j<=k;j++)
            f[i+sz[dfn[i]]][j]=max(f[i][j],f[i+sz[dfn[i]]][j]);
	}
	for(int i=dfc-1;i;i--)
	{
        for(int j=0;j<k;j++)
            g[i][j+1]=max(g[i][j+1],g[i+1][j]+a[dfn[i+1]]);
		for(int j=0;j<=k;j++)
			g[i][j]=max(g[i+sz[dfn[i+1]]][j],g[i][j]);
	}
    for(int i=1;i<=dfc;i++){
        for(int j=0;j<k;j++)
            ans[dfn[i]]=max(ans[dfn[i]],g[i][j]+f[i][k-j-1]+a[dfn[i]]);
    }
    for(auto v : adj[u]){
        if(sz[v]>=k) cal(v);
    }
}
void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cal(1);
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
}

/*
5 5
2 1 2 1 2
1 5
1 6
1 7
2 4 2
1 7
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 6520kb

input:

5 3
6 10 4 3 4
3 4
4 2
2 5
5 1

output:

20 20 0 0 20 

result:

wrong answer 3rd numbers differ - expected: '17', found: '0'