Codeforces 1088E Ehab and a component choosing problem

时间:2023-03-10 02:43:25
Codeforces 1088E Ehab and a component choosing problem

Ehab and a component choosing problem

如果有多个连接件那么这几个连接件一定是一样大的, 所以我们先找到值最大的连通块这个肯定是分数的答案。

dp[ i ]表示对于 i 这棵子树包含 i 这个点的连通块的最大值, 就能求出答案, 然后知道最大值之后再就能求出几个连接件。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 3e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, ans, a[N];
LL dp[N], mx = -INF;
vector<int> G[N]; void getMax(int u, int fa) {
dp[u] = a[u];
for(auto& v : G[u]) {
if(v == fa) continue;
getMax(v, u);
if(dp[v] > ) dp[u] += dp[v];
}
} void getAns(int u, int fa) {
dp[u] = a[u];
for(auto& v : G[u]) {
if(v == fa) continue;
getAns(v, u);
if(dp[v] > ) dp[u] += dp[v];
}
if(dp[u] == mx) ans++, dp[u] = ;
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
getMax(, );
for(int i = ; i <= n; i++) mx = max(mx, dp[i]);
getAns(, );
printf("%lld %d\n", mx * ans, ans);
return ;
} /*
*/