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 = 200005;
int n, dp[N], s[N], pa[N], ans = 1;
vector<int> adj[N];
void dfs(int u, int p) {
for (int v : adj[u]) {
if (v == p) continue;
pa[v] = u;
dfs(v, u);
dp[u] += max(s[v], dp[v]);
ans = max(ans, max(s[v], dp[v]) + s[u]);
}
dp[u] -= s[u];
if (u != 1) ans = max(ans, dp[u] + s[pa[u]]);
else ans = max(ans, dp[u]);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
s[i] = c - '0';
}
dfs(1, -1);
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... |