Submission #668143

#TimeUsernameProblemLanguageResultExecution timeMemory
668143stevancvPower Plant (JOI20_power)C++14
0 / 100
4 ms5076 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 2e5 + 2; const int inf = 2e9; vector<int> g[N]; int dp[N][2]; int str[N]; void Dfs(int s, int e) { dp[s][0] = -str[s]; dp[s][1] = -inf; for (int u : g[s]) { if (u == e) continue; Dfs(u, s); dp[s][0] += max(dp[u][0], dp[u][1] - 2); if (dp[u][1] >= 2) smax(dp[s][1], dp[u][1] - 2 * str[s]); if (str[s] == 1 && dp[u][0] >= 1) smax(dp[s][1], dp[u][0] + 1); } smax(dp[s][0], str[s]); //cout << s + 1 << " : " << dp[s][0] << sp << dp[s][1] << en; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u -= 1; v -= 1; g[u].push_back(v); g[v].push_back(u); } string sr; cin >> sr; for (int i = 0; i < n; i++) str[i] = sr[i] - '0'; Dfs(0, -1); cout << max(dp[0][0], dp[0][1]) << en; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...