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>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
string str;
cin >> str;
vector<bool> active(n);
for (int i = 0; i < n; ++i) {
active[i] = str[i] - '0';
}
if (active == vector(n, false)) {
cout << 0 << '\n';
return 0;
}
vector<int> dp(n);
function<void(int, int)> dfs = [&](int v, int p) {
for (int to : adj[v]) {
if (to != p) {
dfs(to, v);
dp[v] += dp[to];
}
}
dp[v] -= active[v];
if (active[v]) {
dp[v] = max(dp[v], 1);
}
dp[v] = max(dp[v], 0);
};
dfs(0, -1);
int ans = 1;
function<void(int, int, int)> gfs = [&](int v, int p, int up) {
int sum = 0;
for (int to : adj[v]) {
if (to != p) {
sum += dp[to];
}
}
ans = max(ans, sum + up - active[v]);
sum = sum + up - active[v];
for (int to : adj[v]) {
if (to != p) {
int k = max(sum - dp[to], int(active[v]));
ans = max(ans, dp[to] + k);
gfs(to, v, k);
}
}
};
gfs(0, -1, 0);
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |