# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
295458 | dsabolic | Power Plant (JOI20_power) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
using namespace std;
const int N = 2e5 + 50;
int n;
vector<int> graph[N];
string s;
int dp[N];
int sol = 0;
int rek(int v, int p) {
int sum = 0, mx = 0, cnt = 0;
for(auto n : graph[v]) {
if(n == p) continue;
int val = rek(n, v);
if(val) cnt++;
sum += val;
mx = max(mx, val);
}
//if(s[v] == '0' || cnt > 1) dp[v] = max(s[v] - '0', sum - s[v] + '0');
dp[v] = max(s[v] - '0', sum - (s[v] - '0'));
else dp[v] = max(1, sum);
if(s[v] == '1') {
sol = max(sol, max(mx + 1, dp[v]));
} else {
sol = max(sol, dp[v]);
}
return dp[v];
}
int main() {
cin >> n;
rep(i, 0, n - 1) {
int a, b;
cin >> a >> b;
--a, --b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> s;
rek(0, -1);
printf("%d\n", sol);
return 0;
}