https://cn.vjudge.net/problem/CodeForces-1088E
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
思路:很明显,这k个联通块都应该是最大联通块(从权值来看),所以先求出最大权值,看有多少个联通块的权值等于最大权值(用过了就不能再用了,注意标记)
#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int N=3e5+10;
struct Edge{
int v,next;
}e[N<<1];
int head[N],tot;
ll mx,k;
void init(int n){
tot=0;
for(int i=1;i<=n;i++) head[i]=-1;
mx=-1e18;
k=0;
}
void add(int u,int v){
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
}
ll dp[N],a[N];
void dfs1(int u,int fa){
dp[u]=a[u];
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].v;
if(v==fa) continue;
dfs1(v,u);
if(dp[v]>0) dp[u]+=dp[v];
}
mx=max(mx,dp[u]);
}
void dfs2(int u,int fa){
dp[u]=a[u];
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].v;
if(v==fa) continue;
dfs2(v,u);
if(dp[v]>0) dp[u]+=dp[v];
}
if(dp[u]==mx){
k++;
dp[u]=-1e18;
}
}
int main(){
int n;
scanf("%d",&n);
init(n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs1(1,0);
dfs2(1,0);
printf("%lld %lld\n",mx*k,k);
return 0;
}