This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: shafwanur010
* created: 11/11/2020 10:23:47 PM (Bangladesh Standard Time)
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n + 1);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
string s;
cin >> s;
s = '0' + s;
int ans = 0;
vector<int> dp(n + 1);
function<void(int, int)> dfs = [&](int u, int p) {
int sum = 0;
for (int v : g[u]) {
if (v != p) {
dfs(v, u);
sum += dp[v];
}
}
dp[u] = max(s[u] - '0', sum - (s[u] - '0'));
ans = max({ans, dp[u], dp[u] + (s[p] - '0')});
};
dfs(1, 0);
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... |