#include <bits/stdc++.h>
#define fastio ios_base :: sync_with_stdio(0), cin.tie(0);
using namespace std;
using ll = long long;
const int mxN = 1e6 + 5, mod = 1e9 + 7;
int n, d[mxN], b[mxN], ans; string a;
vector <int> adj[mxN];
void calc(int v, int p = -1) {
for (auto to : adj[v]) {
if (to != p) calc(to, v), d[v] += d[to], b[v] |= b[to];
}
d[v] += a[v - 1] * (b[v] > 0 ? -1 : 1);
b[v] |= a[v - 1];
if (!d[v])
d[v] = b[v] = a[v - 1];
}
void dfs(int v, int p = -1) {
int cnt = 0, h = 0;
for (auto to : adj[v])
cnt += d[to], h += b[to];
ans = max(ans, (int)a[v - 1]);
ans = max(ans, cnt + (h > 0 ? -a[v - 1] : a[v - 1]));
for (auto to : adj[v]) {
if (to != p) {
int dv = d[v], dto = d[to];
int bv = b[v], bto = b[to];
d[v] -= d[to];
if (d[v] <= 1) {
if (a[v - 1])
d[v] = b[v] = 1;
else {
if (d[v] <= 0)
b[v] = 0;
}
}
if (d[to] == 1 && a[to - 1] == 1)
d[to] = b[to] = 0;
d[to] += d[v]; b[to] |= b[v];
d[to] += a[to - 1] * (b[to] > 0 ? -1 : 1);
b[to] |= a[to - 1];
if (!d[to])
d[to] = b[to] = a[to - 1];
dfs(to, v);
d[v] = dv, d[to] = dto, b[v] = bv, b[to] = bto;
}
}
}
int main() {
fastio;
cin >> n;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> a;
for (auto &x : a) x -= '0';
calc(1); dfs(1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
16 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23780 KB |
Output is correct |
8 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
16 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23780 KB |
Output is correct |
8 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
16 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23780 KB |
Output is correct |
8 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |