Submission #1054976

#TimeUsernameProblemLanguageResultExecution timeMemory
1054976SharkyPower Plant (JOI20_power)C++17
100 / 100
66 ms30036 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, dp[N], s[N], pa[N], ans = 1; vector<int> adj[N]; void dfs(int u, int p) { for (int v : adj[u]) { if (v == p) continue; pa[v] = u; dfs(v, u); dp[u] += max(s[v], dp[v]); ans = max(ans, max(s[v], dp[v]) + s[u]); } dp[u] -= s[u]; if (u != 1) ans = max(ans, dp[u] + s[pa[u]]); else ans = max(ans, dp[u]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { char c; cin >> c; s[i] = c - '0'; } dfs(1, -1); cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...