#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;
for (int u : g[s]) {
if (u == e) continue;
Dfs(u, s);
dp[s][0] += max(dp[u][0], dp[u][1] - 2);
if (dp[u][1] >= 2) smax(dp[s][1], dp[u][1] - 2 * str[s]);
if (str[s] == 1 && dp[u][0] >= 1) smax(dp[s][1], dp[u][0] + 1);
}
smax(dp[s][0], str[s]);
//cout << s + 1 << " : " << dp[s][0] << sp << dp[s][1] << en;
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
2 ms |
5024 KB |
Output is correct |
4 |
Correct |
4 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
2 ms |
5024 KB |
Output is correct |
4 |
Correct |
4 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
2 ms |
5024 KB |
Output is correct |
4 |
Correct |
4 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |