Codeforces Round #525 (Div. 2)—E. Ehab and a component choosing problem(树形dp)

该博客介绍了如何解决一个关于树形动态规划的问题,即在给定树结构和节点权重的情况下,选择一定数量的不重叠连通组件以最大化节点权重之和除以连通组件数目的值。题目要求在保持分数最大化的同时,尽可能选择更多的连通组件。文章通过示例解释了题意,并给出了解决方案的思路和代码实现,强调了避免组件重叠和考虑分母最大化的重要性。
摘要由CSDN通过智能技术生成

E. Ehab and a component choosing problem

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You're given a tree consisting of n

nodes. Every node u has a weight au. You want to choose an integer k (1≤k≤n) and then choose k connected components of nodes that don't overlap (i.e every node is in at most 1 component). Let the set of nodes you chose be s

. You want to maximize:

 

 

 

 

In other words, you want to maximize the sum of weights of nodes in s

divided by the number of connected components you chose. Also, if there are several solutions, you want to maximize k

.

Note that adjacent nodes can belong to different components. Refer to the third sample.

Input

The first line contains the integer n

(1≤n≤3⋅105)

, the number of nodes in the tree.

The second line contains n

space-separated integers a1, a2, …, an (|ai|≤109)

, the weights of the nodes.

The next n−1

lines, each contains 2 space-separated integers u and v (1≤u,v≤n) which means there's an edge between u and v

.

Output

Print the answer as a non-reduced fraction represented by 2 space-separated integers. The fraction itself should be maximized and if there are several possible ways, you should maximize the denominator. See the samples for a better understanding.

Examples

Input

Copy

3
1 2 3
1 2
1 3

Output

Copy

6 1

Input

Copy

1
-2

Output

Copy

-2 1

Input

Copy

3
-1 -1 -1
1 2
2 3

Output

Copy

-3 3

Input

Copy

3
-1 -2 -1
1 2
1 3

Output

Copy

-2 2

Note

A connected component is a set of nodes such that for any node in the set, you can reach all other nodes in the set passing only nodes in the set.

In the first sample, it's optimal to choose the whole tree.

In the second sample, you only have one choice (to choose node 1) because you can't choose 0 components.

In the third sample, notice that we could've, for example, chosen only node 1, or node 1 and node 3, or even the whole tree, and the fraction would've had the same value (-1), but we want to maximize k

.

In the fourth sample, we'll just choose nodes 1 and 3.

题意:就是找k个连通块。联通块的代价就是节点的总和。求图中公式的最大值。

连通块不能有相交的地方。

思路:肯定是分母越大,分子越小这个值最大。那么就是这个联通块的值最大,最大的,连通块数目肯定小。看有几个。

第一遍dfs,预处理出u节点为根的最大值。dp[u].

第二遍dfs2.当这个值为ans时,将其dp[v]置为0,这样就不会有重叠了,数数几个。

注意longlong

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=300000+100;
const int inf=0x3f3f3f3f;
typedef long long LL;
int n;
LL ans;
int flag;
LL a[maxn],dp[maxn];
LL cnt;
vector<LL>vec[maxn];
void dfs(int u,int fa){
    dp[u]=a[u];
    int num=vec[u].size();
    for(int i=0;i<num;i++){
        int v=vec[u][i];
        if(v==fa)
            continue;
        dfs(v,u);
        if(dp[v]>0)
            dp[u]+=dp[v];
    }
    ans=max(ans,dp[u]);
}
void dfs2(int u,int fa){
      dp[u]=a[u];
    int num=vec[u].size();
    for(int i=0;i<num;i++){
        int v=vec[u][i];
        if(v==fa)
            continue;
        dfs2(v,u);
        if(dp[v]>0)
            dp[u]+=dp[v];
    }
    if(dp[u]==ans){
        dp[u]=0;
        cnt++;
    }
}
int main()
{
    LL u,v;
   while(scanf("%d",&n)!=EOF){
    memset(vec,0,sizeof(vec));
   memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    for(int i=1;i<=n-1;i++){
        scanf("%lld%lld",&u,&v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    ans=-inf;cnt=0;
    dfs(1,-1);
    dfs2(1,-1);
    long long pp=ans*cnt;
    cout<<pp<<" "<<cnt<<endl;
   }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值