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;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
const int N = 200000;
vector <int> adj[N];
int sz[N], dp[N], ans[N];
string s;
void dfs(int v, int pa) {
dp[v] = s[v] == '1';
int tot = 0, mx = 0;
for (int u : adj[v]) if (u != pa) {
dfs(u, v);
tot += dp[u], mx = max(mx, dp[u]);
}
dp[v] = max(dp[v], tot - (s[v] == '1'));
ans[v] = max(dp[v], mx + (s[v] == '1'));
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
for (int i = 0, u, v; i < n - 1; ++i) {
cin >> u >> v, --u, --v;
adj[u].pb(v), adj[v].pb(u);
}
cin >> s;
dfs(0, -1);
cout << *max_element(ans, ans + n) << '\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... |