Submission #331881

#TimeUsernameProblemLanguageResultExecution timeMemory
331881phathnvPower Plant (JOI20_power)C++11
100 / 100
214 ms30508 KiB
#include <bits/stdc++.h> #define mp make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair <int, int> ii; const int N = 2e5 + 1; int n; vector <int> adj[N]; string s; int a[N], dp[N][2]; void readInput(){ cin >> n; for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } cin >> s; } void dfs(int u, int p){ dp[u][0] = -a[u]; dp[u][1] = a[u]; int maxVal = 0; for(int v : adj[u]){ if (v == p) continue; dfs(v, u); dp[u][0] += max(a[v], dp[v][0]); maxVal = max(maxVal, dp[v][0]); maxVal = max(maxVal, a[v]); } dp[u][1] += maxVal; dp[u][0] = max(dp[u][0], dp[u][1] - 2 * a[u]); } void solve(){ for(int i = 1; i <= n; i++) a[i] = s[i - 1] - '0'; dfs(1, -1); int res = 0; for(int i = 1; i <= n; i++){ //cerr << i << ' ' << dp[i][0] << ' ' << dp[i][1] << endl; res = max(res, dp[i][0]); res = max(res, dp[i][1]); } cout << res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); readInput(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...