#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> adj[N];
int dp[N], rsm[N], sm[N];
string s;
void dfs(int u, int p = -1) {
dp[u] = -(s[u] == '1'); // don't cut
sm[u] = 0;
for (int v: adj[u]) if (v != p) {
dfs(v, u);
sm[u] += dp[v];
}
dp[u] = max(-dp[u], dp[u] + sm[u]);
}
void reroot(int u, int p = -1) {
if (~p) {
int vp = s[p] == '1' ? 1 : 0;
rsm[u] = sm[u] + max(vp, -vp + rsm[p] - dp[u]);
} else {
rsm[u] = sm[u];
}
int vu = s[u] == '1' ? 1 : 0;
dp[u] = max(vu, rsm[u] - vu);
for (int v: adj[u]) if (v != p) {
reroot(v, u);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
int ans = 0;
dfs(0);
reroot(0);
for (int i = 0; i < n; i++)
ans = max(ans, dp[i]);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Incorrect |
4 ms |
4992 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Incorrect |
4 ms |
4992 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Incorrect |
4 ms |
4992 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |