#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN];
int dp[mxN][2];
string s;
void dfs(int u, int p) {
for (int v : ad[u]) {
if (v == p) continue;
dfs(v, u);
}
if (s[u] == '0') {
for (int v : ad[u]) {
if (v == p) continue;
dp[u][0] += dp[v][0];
dp[u][1] += max(dp[v][0], dp[v][1]);
}
} else {
dp[u][0] = 1;
for (int v : ad[u]) {
if (v == p) continue;
dp[u][1] = max(dp[u][1], dp[v][0] + 1);
dp[u][1] = max(dp[u][1], dp[v][1] - 1);
}
int a = 0, b = 0;
for (int v : ad[u]) {
if (v == p) continue;
a += dp[v][0];
b += dp[v][1] - 1;
}
dp[u][0] = max(dp[u][0], a - 1);
dp[u][1] = max(dp[u][1], b - 1);
}
}
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;
for (int i = 1; i <= n; i++) {
if (s[i] == '1') {
dfs(i, -1);
cout << max(dp[i][1], dp[i][0]) << "\n";
break;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
5028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
5028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
5028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |