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 int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 200005;
int n, u, v, p[MN], dp[MN], ans;
char c;
vector<int> a[MN];
void dfs(int cur, int par, int rt) {
int sum = 0;
for (int nxt : a[cur]) {
if (nxt == par) continue;
dfs(nxt, cur, rt);
sum += dp[nxt];
}
if (p[cur] && cur == rt) {
if (a[rt].size() == 1) dp[cur] = sum + 1;
else dp[cur] = max(1ll, sum - 1);
}
else if (p[cur]) dp[cur] = max(1ll, sum - 1);
else dp[cur] = sum;
}
bool cmp(int x, int y) {
return a[x].size() > a[y].size();
}
int32_t main() {
boost();
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
for (int i = 1; i <= n; i++) cin >> c, p[i] = c - '0';
vector<int> vec;
for (int i = 1; i <= n; i++) vec.push_back(i);
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < min(n, 1000ll); i++) {
dfs(vec[i], 0, vec[i]);
ans = max(ans, dp[vec[i]]);
}
printf("%lld\n", ans);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |