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 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); 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] > 0) 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] > 0) dp[u] += dp[v]; } if(dp[u] == mx) ans++, dp[u] = 0; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 2; i <= n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } getMax(1, 0); for(int i = 1; i <= n; i++) mx = max(mx, dp[i]); getAns(1, 0); printf("%lld %d\n", mx * ans, ans); return 0; } /* */