Submission #495739

#TimeUsernameProblemLanguageResultExecution timeMemory
495739daongochaPower Plant (JOI20_power)C++14
0 / 100
3 ms4940 KiB
#include <bits/stdc++.h> #define ll long long #define fileopen(a, b) freopen(((std::string)a + ".inp").c_str(), "r", stdin); freopen(((std::string)b + ".out").c_str(), "w", stdout); #define fileopen1(a) freopen(((std::string)a + ".inp").c_str(), "r", stdin); freopen(((std::string)a + ".out").c_str(), "w", stdout); using namespace std; const int MAXN = 2e5 + 5; int dp[MAXN]; vector<int> path[MAXN]; int a[MAXN]; void reroot(int u, int v) { dp[u] -= max(dp[v], a[v]); dp[v] += max(dp[u], a[u]); } void dfs(int u, int pre = 0) { dp[u] = -a[u]; for (int v: path[u]) if (v != pre) { dfs(v, u); dp[u] += max(dp[v], a[v]); } } int ans = 0; void calc(int u, int pre = 0) { ans = max(ans, max(dp[u], a[u])); for (int v: path[u]) if (v != pre) { reroot(u, v); calc(v, u); reroot(v, u); } } signed main() { #ifndef PICHU_LOCAL_DEF //fileopen1("LAH"); #endif cin.tie(0)->sync_with_stdio(0); int n; cin >> n; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; path[u].push_back(v); path[v].push_back(u); } for (int i = 1; i <= n; i++) { char c; cin >> c; a[i] = c - '0'; } dfs(1); calc(1); cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...