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

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/85010676

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;
   }
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/85010676