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 ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 10, inf = 2e9;
int n, m, d[MAXN], dp[MAXN], a[MAXN];
vector <int> g[MAXN];
void dfs(int u, int p) {
int smd = 0, mxdp = 0, mxd = 0;
for (int to : g[u]) {
if (to == p)
continue;
dfs(to, u);
smd += d[to];
mxdp = max(mxdp, dp[to]);
mxd = max(mxd, d[to]);
}
d[u] = dp[u] = 0;
d[u] = max(d[u], max(0, -a[u] + smd));
dp[u] = max(dp[u], max(mxdp, smd));
if (a[u] == 1) {
d[u] = max(d[u], 1);
dp[u] = max(dp[u], 1 + mxd);
}
}
main() {
// freopen("input.txt", "r", stdin);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for (int i = 1; i <= n; i++) {
char ch;
cin >> ch;
a[i] = ch - '0';
}
dfs(1, -1);
cout << dp[1];
}
Compilation message (stderr)
power.cpp:33:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
33 | main() {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |