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>
#define maxn 200005
using namespace std;
int n;
vector<int> adj[maxn];
int f[maxn], g[maxn];
char a[maxn];
int ds = 0;
int E[maxn];
void dfs(int u, int dad) {
int sum = 0, mx = 0;
for (int v : adj[u]) {
if (v == dad) continue;
dfs(v, u);
mx = max(mx, f[v]);
sum += f[v];
}
if (E[u] == 1) ds = max(ds, max(mx+1, sum-1));
else ds = max(ds, sum);
f[u] = max(E[u], sum-E[u]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == '1') E[i] = 1;
}
dfs(1, 0);
cout << ds;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |