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;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 2e5 + 5;
bool s[N];
int dp[N][2];
vector<int> g[N];
void dfs(int v, int p) {
dp[v][0] = dp[v][1] = s[v];
int sum = 0;
for (int u : g[v]) {
if (u == p) continue;
dfs(u, v);
sum += dp[u][1];
dp[v][0] = max(dp[v][0], dp[u][0]);
dp[v][0] = max(dp[v][0], s[v] + dp[u][1]);
}
dp[v][0] = max(dp[v][0], sum - s[v]);
dp[v][1] = max(dp[v][1], sum - s[v]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
char c;
cin >> c;
s[i] = c - '0';
}
dfs(1, 0);
cout << dp[1][0] << '\n';
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... |