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;
const int N = 2e5 + 5;
vector<int> adj[N];
int dp[N], rsm[N], sm[N], val[N];
int dfs(int u, int p = -1) {
dp[u] = -val[u]; // don't cut
for (int v: adj[u]) if (v != p)
sm[u] += dfs(v, u);
return dp[u] = max(-dp[u], dp[u] + sm[u]);
}
void reroot(int u, int p = -1) {
rsm[u] = sm[u] + (~p ? max(val[p], -val[p] + rsm[p] - dp[u]) : 0);
dp[u] = max(val[u], rsm[u] - val[u]);
for (int v: adj[u]) if (v != p)
reroot(v, u);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
string s; cin >> s;
int ans = 0;
for (int i = 0; i < n; i++)
ans += val[i] = (s[i] == '1');
ans = min(ans, 2);
dfs(0);
reroot(0);
for (int i = 0; i < n; i++)
ans = max(ans, dp[i]);
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |