Submission #640172

#TimeUsernameProblemLanguageResultExecution timeMemory
640172colossal_pepePower Plant (JOI20_power)C++17
100 / 100
548 ms62496 KiB
#include <iostream> #include <vector> #include <map> using namespace std; int n; vector<vector<int>> g; string s; vector<map<int, int>> dp; vector<int> dp_sum; int f(int a, int b) { return (s[a] == '1' ? max(1, dp_sum[a] - dp[a][b] - 1) : dp_sum[a] - dp[a][b]); } int dfs1(int u, int par) { dp[u][par] = 0; for (int v : g[u]) { if (v != par) { dp[u][v] = dfs1(v, u); dp_sum[u] += dp[u][v]; } } return f(u, par); } void dfs2(int u, int par) { dp[u][par] = f(par, u); dp_sum[u] += dp[u][par]; for (int v : g[u]) { if (v != par) dfs2(v, u); } } int solve() { dp.resize(n), dp_sum.resize(n, 0); for (int v : g[0]) { dp[0][v] += dfs1(v, 0); dp_sum[0] += dp[0][v]; } for (int v : g[0]) { dfs2(v, 0); } int ans = 0; for (int u = 0; u < n; u++) { for (int v : g[u]) { ans = max(ans, f(u, v) + f(v, u)); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; g.resize(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } cin >> s; cout << max(1, solve()) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...