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, dp[u] + s[u]);
dp[u] -= s[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';
}
for (int i = 1; i <= n; i++) for (auto j : adj[i]) if (s[i] + s[j] == 2) ans = 2;
dfs(1, -1);
for (int i = 2; i <= n; i++) ans = max(ans, s[pa[i]] + 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... |