CodeForces - 1088E Ehab and a component choosing problem 树形dp

You're given a tree consisting of nn nodes. Every node uu has a weight auau. You want to choose an integer kk (1≤k≤n)(1≤k≤n) and then choose kk 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 ss. You want to maximize:

 

∑u∈sauk∑u∈sauk

In other words, you want to maximize the sum of weights of nodes in ss divided by the number of connected components you chose. Also, if there are several solutions, you want to maximize kk.

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

Input

The first line contains the integer nn (1≤n≤3⋅105)(1≤n≤3⋅105), the number of nodes in the tree.

The second line contains nn space-separated integers a1a1, a2a2, ……, anan (|ai|≤109)(|ai|≤109), the weights of the nodes.

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

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

3
1 2 3
1 2
1 3

Output

6 1

Input

1
-2

Output

-2 1

Input

3
-1 -1 -1
1 2
2 3

Output

-3 3

Input

3
-1 -2 -1
1 2
1 3

Output

-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 kk.

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

题意:给你一棵数,选出k个联通块,让  k个联通块内所有点权值加和 /  k  最大,若结果多个  让k最大,输出权值和 和 k

题解:先找出最大的联通块,然后看看有几个符合的联通块,注意,符合后注意标记

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define PI acos(-1)
const int N = 3e5+10;

ll dp[N], a[N];
int n;
ll maxx,k;
vector<int> v[N];
void dfsI(int f,int u)
{
    dp[u]=a[u];
    for(int i=0;i<v[u].size();i++)
    {
        int to=v[u][i];
        if(to==f) continue;
        dfsI(u,to);
        dp[u]+=max(dp[to],0ll);
    }
    maxx=max(dp[u],maxx);
}
void dfsII(int f,int u)
{
    dp[u]=a[u];
    for(int i=0;i<v[u].size();i++)
    {
        int to=v[u][i];
        if(to==f) continue;
        dfsII(u,to);
        dp[u]+=max(dp[to],0ll);
    }
    if(dp[u]==maxx) k++,dp[u]=-1;
}
int main()
{
    int x,y;
    while(~scanf("%d", &n))
    {
        maxx=-1e18;
        k=0;
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]),v[i].clear();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        dfsI(0,1);
        dfsII(0,1);
        printf("%lld %lld\n",maxx*k,k);
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值