Submission #542131

#TimeUsernameProblemLanguageResultExecution timeMemory
542131bluePower Plant (JOI20_power)C++17
100 / 100
193 ms40328 KiB
#include <iostream> #include <algorithm> #include <vector> using namespace std; using vi = vector<int>; using vvi = vector<vi>; const int mx = 200'000; int N; vi edge[1+mx]; int DP[1+mx]; vi power; vi children[1+mx]; int res = 1; void dfs(int u, int p) { for(int v : edge[u]) { if(v == p) continue; children[u].push_back(v); dfs(v, u); } if(power[u]) DP[u] = 1; else DP[u] = 0; int curr = -power[u]; for(int v : children[u]) curr += max(0, DP[v]); DP[u] = max(DP[u], curr); for(int v : children[u]) res = max(res, power[u] + DP[v]); curr = -power[u]; for(int v : children[u]) curr += max(0, DP[v]); res = max(res, curr); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N; for(int e = 1; e <= N-1; e++) { int a, b; cin >> a >> b; edge[a].push_back(b); edge[b].push_back(a); } power = vi(1+N); for(int i = 1; i <= N; i++) { char c; cin >> c; if(c == '1') power[i] = 1; else power[i] = 0; } dfs(1, 0); cout << res << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...