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>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN];
string s;
int dp[mxN];
int ans = 0;
void dfs(int u, int p) {
for (int v : ad[u]) {
if (v == p) continue;
dfs(v, u);
}
int x = 0;
int val = s[u] - '0';
dp[u] = val;
for (int v : ad[u]) {
if (v == p) continue;
dp[u] = max(dp[u], dp[v] - val);
ans = max(ans, dp[v] + val);
x += dp[v];
}
dp[u] = max(dp[u], x - val);
ans = max(ans, dp[u]);
}
void testCase() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
cin >> s;
s = '#' + s;
dfs(1, -1);
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
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... |