#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
int profit[N], ans;
vector<int> adj[N];
string power;
void dfs_pull(int u, int p = 0) {
profit[u] = '0' - power[u];
for (auto c : adj[u]) {
if (c != p) {
dfs_pull(c, u);
profit[u] += profit[c];
}
}
profit[u] = max(profit[u], power[u] - '0');
}
void dfs_push(int u, int p = 0) {
profit[u] = '0' - power[u];
for (auto c : adj[u]) {
profit[u] += profit[c];
}
ans = max(ans, profit[u]);
for (auto c : adj[u]) {
if (c != p) {
int prv = profit[u];
profit[u] = max(profit[u] - profit[c], power[u] - '0');
dfs_push(c, u);
profit[u] = prv;
}
}
}
int main() {
ios_base::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;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> power;
power = " " + power;
dfs_pull(1);
dfs_push(1);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |