Codeforces Round #525 (Div. 2) D

The meaning of problems

Choose from a tree k k a communication block, such that a i k \frac{\sum a_i}{k} As large as possible, in as large basis, let k k as large as possible

answer

We consider, apparently reverse platoon collection, if you have chosen k k a, the first to choose k + 1 k+1
t = S k S + w k + 1 t=\frac{S}{k}-\frac{S+w}{k+1}
Compare the size, because k w S kw \ leq S
Order t 0 t \ geq 0 , you will find k w S kw \ leq S , which is known conditions, only a selected that is optimal.
but k k be the better, so that a plurality of maximum value selected from a set of requirements.

Seeking to i i is a root node, the maximum value of this must take down the point (same set)
clearly has:
f i = m a x ( f j , 0 ) f_i=\sum max(f_j,0)

Get the maximum sum, pitted again d p dp , each time the same as the maximum and that, k + + k++ As the current d p dp is equal to negative infinity, then backtracking. That can be guaranteed not to overlap.

#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 405000;

int n,A[maxn];
vector<int>G[maxn];
ll dp[maxn],ans=-inf,k;

void dfs1(int u,int f){
    dp[u]=A[u];
    for(auto v:G[u]){
        if(v==f)continue;
        dfs1(v,u);
        dp[u]+=max(dp[v],1ll*0);
    }
    ans=max(ans,dp[u]);
}

void dfs2(int u,int f){
    dp[u]=A[u];
    for(auto v:G[u]){
        if(v==f)continue;
        dfs2(v,u);
        dp[u]+=max(dp[v],1ll*0);
    }
    if(dp[u]==ans){
        k++;
        dp[u]=-inf;
    }
}

int main(){
    cin>>n;
    FOR(i,1,n)dp[i]=-inf;
    FOR(i,1,n)scanf("%d",&A[i]);
    FOR(i,1,n-1){
        int u,v;
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs1(1,-1);
    dfs2(1,-1);
    cout<<ans*k<<" "<<k<<endl;
}

Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104223173