Submission #683884

#TimeUsernameProblemLanguageResultExecution timeMemory
683884abc864197532Power Plant (JOI20_power)C++17
100 / 100
146 ms27852 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int,int> #define all(x) x.begin(), x.end() const int N = 200000; vector <int> adj[N]; int sz[N], dp[N], ans[N]; string s; void dfs(int v, int pa) { dp[v] = s[v] == '1'; int tot = 0, mx = 0; for (int u : adj[v]) if (u != pa) { dfs(u, v); tot += dp[u], mx = max(mx, dp[u]); } dp[v] = max(dp[v], tot - (s[v] == '1')); ans[v] = max(dp[v], mx + (s[v] == '1')); } int main() { ios::sync_with_stdio(false), cin.tie(0); int n; cin >> n; for (int i = 0, u, v; i < n - 1; ++i) { cin >> u >> v, --u, --v; adj[u].pb(v), adj[v].pb(u); } cin >> s; dfs(0, -1); cout << *max_element(ans, ans + n) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...