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

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 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:

扫描二维码关注公众号,回复: 4708656 查看本文章

∑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

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

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

题意:

给你n个结点的权值,再给你n-1条无向无权边(保证构成一棵树),让你找k个互不相交的连通块,使得这k个连通块中的每个连通块的节点的权值和都相等且最大。也就是说,找一个最大权值的连通块,然后找与它互不相交的连通块且权值相同的有几个。输出这些连通块的总权值和连通块的个数。

思路:

感觉就是纯阅读理解,读懂题意之后就变得很简单了:

利用贪心思想,先dp一遍求出以i为根的连通块的最大权值,然后再dp一遍求有多少个和最大值相同的连通块即可。

代码中的许多细节还需要自己慢慢体会。

代码:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int maxn=3e5+10;
vector<ll>vc[maxn];
ll a[maxn],dp[maxn],c[maxn];
int n;
bool jud;
ll ans,cnt;
ll dfs(int u,int fa)
{
    dp[u]=a[u];
    for(int i=0;i<vc[u].size();i++)
    {
        int v=vc[u][i];
        if(v!=fa) {
        dfs(v,u);
        if(dp[v]>0) dp[u]+=dp[v];
        }
    }
    ans=max(ans,dp[u]);
    if(jud&&dp[u]==ans)
    {
        dp[u]=0;
        cnt++;
    }
    return dp[u];
}
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        vc[x].push_back(y);
        vc[y].push_back(x);
    }
    ans=-inf;cnt=0;
    dfs(1,0);
    jud=1;
    dfs(1,0);
    printf("%lld %lld\n",ans*cnt,cnt);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/84978804