Submission #411278

#TimeUsernameProblemLanguageResultExecution timeMemory
411278nichkePower Plant (JOI20_power)C++14
100 / 100
211 ms31272 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' int n; int cnt; string s; vector<int> adj[200005]; bool pw[200005]; int dp[200005][2]; void dfs(int v, int p = -1) { if (s[v] == '1') dp[v][0] = 1; int tot = 0, mx = 0; for (int u : adj[v]) { if (u == p) continue; dfs(u, v); tot += dp[u][0]; mx = max(mx, dp[u][0]); } dp[v][0] = max(tot - dp[v][0], dp[v][0]); if (s[v] == '1') dp[v][1] = mx + 1; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '1') cnt++; } int ans = 0; dfs(0); for (int i = 0; i < n; i++) { ans = max({ans, dp[i][0], dp[i][1]}); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...