#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
vector<int> g[N];
int dp[N][2];
int str[N];
void Dfs(int s, int e) {
dp[s][0] = str[s];
dp[s][1] = -inf;
int sum = 0;
for (int u : g[s]) if (u != e) {
Dfs(u, s);
sum += dp[u][0];
smax(dp[s][1], dp[u][1] - str[s]);
if (str[s] && dp[u][0]) smax(dp[s][1], dp[u][0] + 1);
}
smax(dp[s][0], sum - str[s]);
int fs = sum;
for (int u : g[s]) if (u != e) {
if (str[s] || sum > dp[u][0]) fs += max(0, dp[u][1] - 2 - dp[u][0]);
}
smax(dp[s][0], fs - str[s]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u -= 1;
v -= 1;
g[u].push_back(v);
g[v].push_back(u);
}
string sr;
cin >> sr;
for (int i = 0; i < n; i++) str[i] = sr[i] - '0';
Dfs(0, -1);
cout << max(dp[0][0], dp[0][1]) << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4932 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 |
5020 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4932 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 |
5020 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4932 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 |
5020 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |