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 i64 = long long;
constexpr int N = 200000;
int n;
std::vector<int> e[N];
int a[N], dp[N];
void dfs(int u, int p) {
int sum = 0;
for (int v : e[u]) {
if (v != p) {
dfs(v, u);
sum += dp[v];
}
}
dp[u] = std::max(a[u], sum - a[u]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
std::string S;
std::cin >> S;
for (int i = 0; i < n; i++) {
a[i] = S[i] - '0';
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j : e[i]) {
ans = std::max(ans, a[i] + a[j]);
}
}
for (int i = 0; i < n; i++) {
dfs(i, -1);
ans = std::max(ans, dp[i]);
}
std::cout << ans << "\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... |