#include <bits/stdc++.h>
#define ll long long
#define lld long double
#define fi first
#define se second
#define mp make_pair
#define ii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int> >
#define pb push_back
#define eb emplace_back
#define remax(a, b) a = max(a, b);
#define remin(a, b) a = min(a, b);
#define all(x) x.begin(), x.end()
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define bit(n, x) (n & (1 << x))
using namespace std;
int ans = 0;
const int maxn=2e5+1;
vvi edges(maxn);
vvi dp(maxn, vi(2, 0));
string s;
void dfs(int cur, int par) {
int sm0=0, sm1=0, mx0=0, mx1=0;
for (int next : edges[cur]) {
if (next != par) {
dfs(next, cur);
sm0 += max(0, dp[next][0]);
sm1 += max(0, dp[next][1]);
remax(mx0, dp[next][0]);
remax(mx1, dp[next][1]);
}
}
if (s[cur] == '1') { //if there is
dp[cur][1] = max({1, sm1-1});
dp[cur][0] = max({1, sm1-1, mx0, mx1+1});
} else {
dp[cur][1] = max({sm1});
dp[cur][0] = max({sm1, mx0});
}
//remax(ans, dp[cur][0]);
}
int main() {
fastio();
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a;--b;
edges[a].eb(b);
edges[b].eb(a);
}
cin >> s;
dfs(0, -1);
/*for (int i = 0; i < n; ++i) {
cout << i+1 << ' ' << dp[i][0] << ' ' << dp[i][1] << endl;
}*/
cout << dp[0][0] << '\n';
return 0;
}
/* --3
6
2 3
4 3
1 3
3 5
6 2
110011
*/
/* --3
8
1 2
3 5
6 4
4 5
5 2
7 2
2 8
11111111
*/
/* --5
16
7 10
5 11
9 4
14 12
2 11
14 16
4 2
1 13
11 3
7 1
15 9
2 1
11 6
14 9
8 9
0111111001001110
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15960 KB |
Output is correct |
2 |
Correct |
10 ms |
16080 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15964 KB |
Output is correct |
5 |
Correct |
10 ms |
15960 KB |
Output is correct |
6 |
Correct |
11 ms |
15964 KB |
Output is correct |
7 |
Correct |
10 ms |
16096 KB |
Output is correct |
8 |
Correct |
10 ms |
15964 KB |
Output is correct |
9 |
Correct |
9 ms |
15964 KB |
Output is correct |
10 |
Correct |
10 ms |
15960 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
10 ms |
16132 KB |
Output is correct |
13 |
Correct |
10 ms |
16128 KB |
Output is correct |
14 |
Correct |
10 ms |
16012 KB |
Output is correct |
15 |
Correct |
9 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
15960 KB |
Output is correct |
17 |
Correct |
11 ms |
15964 KB |
Output is correct |
18 |
Correct |
10 ms |
16216 KB |
Output is correct |
19 |
Correct |
10 ms |
15964 KB |
Output is correct |
20 |
Correct |
10 ms |
15872 KB |
Output is correct |
21 |
Correct |
11 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15960 KB |
Output is correct |
2 |
Correct |
10 ms |
16080 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15964 KB |
Output is correct |
5 |
Correct |
10 ms |
15960 KB |
Output is correct |
6 |
Correct |
11 ms |
15964 KB |
Output is correct |
7 |
Correct |
10 ms |
16096 KB |
Output is correct |
8 |
Correct |
10 ms |
15964 KB |
Output is correct |
9 |
Correct |
9 ms |
15964 KB |
Output is correct |
10 |
Correct |
10 ms |
15960 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
10 ms |
16132 KB |
Output is correct |
13 |
Correct |
10 ms |
16128 KB |
Output is correct |
14 |
Correct |
10 ms |
16012 KB |
Output is correct |
15 |
Correct |
9 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
15960 KB |
Output is correct |
17 |
Correct |
11 ms |
15964 KB |
Output is correct |
18 |
Correct |
10 ms |
16216 KB |
Output is correct |
19 |
Correct |
10 ms |
15964 KB |
Output is correct |
20 |
Correct |
10 ms |
15872 KB |
Output is correct |
21 |
Correct |
11 ms |
15964 KB |
Output is correct |
22 |
Correct |
11 ms |
15972 KB |
Output is correct |
23 |
Correct |
10 ms |
16216 KB |
Output is correct |
24 |
Correct |
10 ms |
15964 KB |
Output is correct |
25 |
Correct |
10 ms |
15964 KB |
Output is correct |
26 |
Correct |
10 ms |
15964 KB |
Output is correct |
27 |
Correct |
11 ms |
16064 KB |
Output is correct |
28 |
Correct |
10 ms |
16220 KB |
Output is correct |
29 |
Correct |
10 ms |
16220 KB |
Output is correct |
30 |
Correct |
10 ms |
16208 KB |
Output is correct |
31 |
Correct |
11 ms |
15964 KB |
Output is correct |
32 |
Correct |
10 ms |
15960 KB |
Output is correct |
33 |
Correct |
11 ms |
15968 KB |
Output is correct |
34 |
Correct |
10 ms |
15960 KB |
Output is correct |
35 |
Correct |
11 ms |
16008 KB |
Output is correct |
36 |
Correct |
10 ms |
15964 KB |
Output is correct |
37 |
Correct |
10 ms |
16220 KB |
Output is correct |
38 |
Correct |
10 ms |
15964 KB |
Output is correct |
39 |
Correct |
10 ms |
16220 KB |
Output is correct |
40 |
Correct |
11 ms |
16256 KB |
Output is correct |
41 |
Correct |
13 ms |
16136 KB |
Output is correct |
42 |
Correct |
11 ms |
16220 KB |
Output is correct |
43 |
Correct |
10 ms |
16216 KB |
Output is correct |
44 |
Correct |
12 ms |
16216 KB |
Output is correct |
45 |
Correct |
10 ms |
16220 KB |
Output is correct |
46 |
Correct |
10 ms |
16220 KB |
Output is correct |
47 |
Correct |
11 ms |
16232 KB |
Output is correct |
48 |
Correct |
10 ms |
16216 KB |
Output is correct |
49 |
Correct |
10 ms |
16216 KB |
Output is correct |
50 |
Correct |
10 ms |
16220 KB |
Output is correct |
51 |
Correct |
11 ms |
16220 KB |
Output is correct |
52 |
Correct |
11 ms |
16272 KB |
Output is correct |
53 |
Correct |
10 ms |
16220 KB |
Output is correct |
54 |
Correct |
10 ms |
16256 KB |
Output is correct |
55 |
Correct |
10 ms |
16160 KB |
Output is correct |
56 |
Correct |
10 ms |
16220 KB |
Output is correct |
57 |
Correct |
10 ms |
16376 KB |
Output is correct |
58 |
Correct |
10 ms |
16216 KB |
Output is correct |
59 |
Correct |
10 ms |
16216 KB |
Output is correct |
60 |
Correct |
10 ms |
16220 KB |
Output is correct |
61 |
Correct |
11 ms |
16132 KB |
Output is correct |
62 |
Correct |
10 ms |
16220 KB |
Output is correct |
63 |
Correct |
10 ms |
16220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15960 KB |
Output is correct |
2 |
Correct |
10 ms |
16080 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15964 KB |
Output is correct |
5 |
Correct |
10 ms |
15960 KB |
Output is correct |
6 |
Correct |
11 ms |
15964 KB |
Output is correct |
7 |
Correct |
10 ms |
16096 KB |
Output is correct |
8 |
Correct |
10 ms |
15964 KB |
Output is correct |
9 |
Correct |
9 ms |
15964 KB |
Output is correct |
10 |
Correct |
10 ms |
15960 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
10 ms |
16132 KB |
Output is correct |
13 |
Correct |
10 ms |
16128 KB |
Output is correct |
14 |
Correct |
10 ms |
16012 KB |
Output is correct |
15 |
Correct |
9 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
15960 KB |
Output is correct |
17 |
Correct |
11 ms |
15964 KB |
Output is correct |
18 |
Correct |
10 ms |
16216 KB |
Output is correct |
19 |
Correct |
10 ms |
15964 KB |
Output is correct |
20 |
Correct |
10 ms |
15872 KB |
Output is correct |
21 |
Correct |
11 ms |
15964 KB |
Output is correct |
22 |
Correct |
11 ms |
15972 KB |
Output is correct |
23 |
Correct |
10 ms |
16216 KB |
Output is correct |
24 |
Correct |
10 ms |
15964 KB |
Output is correct |
25 |
Correct |
10 ms |
15964 KB |
Output is correct |
26 |
Correct |
10 ms |
15964 KB |
Output is correct |
27 |
Correct |
11 ms |
16064 KB |
Output is correct |
28 |
Correct |
10 ms |
16220 KB |
Output is correct |
29 |
Correct |
10 ms |
16220 KB |
Output is correct |
30 |
Correct |
10 ms |
16208 KB |
Output is correct |
31 |
Correct |
11 ms |
15964 KB |
Output is correct |
32 |
Correct |
10 ms |
15960 KB |
Output is correct |
33 |
Correct |
11 ms |
15968 KB |
Output is correct |
34 |
Correct |
10 ms |
15960 KB |
Output is correct |
35 |
Correct |
11 ms |
16008 KB |
Output is correct |
36 |
Correct |
10 ms |
15964 KB |
Output is correct |
37 |
Correct |
10 ms |
16220 KB |
Output is correct |
38 |
Correct |
10 ms |
15964 KB |
Output is correct |
39 |
Correct |
10 ms |
16220 KB |
Output is correct |
40 |
Correct |
11 ms |
16256 KB |
Output is correct |
41 |
Correct |
13 ms |
16136 KB |
Output is correct |
42 |
Correct |
11 ms |
16220 KB |
Output is correct |
43 |
Correct |
10 ms |
16216 KB |
Output is correct |
44 |
Correct |
12 ms |
16216 KB |
Output is correct |
45 |
Correct |
10 ms |
16220 KB |
Output is correct |
46 |
Correct |
10 ms |
16220 KB |
Output is correct |
47 |
Correct |
11 ms |
16232 KB |
Output is correct |
48 |
Correct |
10 ms |
16216 KB |
Output is correct |
49 |
Correct |
10 ms |
16216 KB |
Output is correct |
50 |
Correct |
10 ms |
16220 KB |
Output is correct |
51 |
Correct |
11 ms |
16220 KB |
Output is correct |
52 |
Correct |
11 ms |
16272 KB |
Output is correct |
53 |
Correct |
10 ms |
16220 KB |
Output is correct |
54 |
Correct |
10 ms |
16256 KB |
Output is correct |
55 |
Correct |
10 ms |
16160 KB |
Output is correct |
56 |
Correct |
10 ms |
16220 KB |
Output is correct |
57 |
Correct |
10 ms |
16376 KB |
Output is correct |
58 |
Correct |
10 ms |
16216 KB |
Output is correct |
59 |
Correct |
10 ms |
16216 KB |
Output is correct |
60 |
Correct |
10 ms |
16220 KB |
Output is correct |
61 |
Correct |
11 ms |
16132 KB |
Output is correct |
62 |
Correct |
10 ms |
16220 KB |
Output is correct |
63 |
Correct |
10 ms |
16220 KB |
Output is correct |
64 |
Correct |
97 ms |
25620 KB |
Output is correct |
65 |
Correct |
89 ms |
25532 KB |
Output is correct |
66 |
Correct |
95 ms |
25644 KB |
Output is correct |
67 |
Correct |
90 ms |
25576 KB |
Output is correct |
68 |
Correct |
97 ms |
25452 KB |
Output is correct |
69 |
Correct |
86 ms |
25476 KB |
Output is correct |
70 |
Correct |
99 ms |
25500 KB |
Output is correct |
71 |
Correct |
89 ms |
25500 KB |
Output is correct |
72 |
Correct |
86 ms |
25488 KB |
Output is correct |
73 |
Correct |
122 ms |
40596 KB |
Output is correct |
74 |
Correct |
106 ms |
40596 KB |
Output is correct |
75 |
Correct |
88 ms |
25384 KB |
Output is correct |
76 |
Correct |
88 ms |
25404 KB |
Output is correct |
77 |
Correct |
120 ms |
25528 KB |
Output is correct |
78 |
Correct |
91 ms |
25396 KB |
Output is correct |
79 |
Correct |
92 ms |
25404 KB |
Output is correct |
80 |
Correct |
93 ms |
25396 KB |
Output is correct |
81 |
Correct |
80 ms |
25672 KB |
Output is correct |
82 |
Correct |
93 ms |
25612 KB |
Output is correct |
83 |
Correct |
104 ms |
31640 KB |
Output is correct |
84 |
Correct |
99 ms |
34140 KB |
Output is correct |
85 |
Correct |
98 ms |
33696 KB |
Output is correct |
86 |
Correct |
92 ms |
26956 KB |
Output is correct |
87 |
Correct |
90 ms |
26252 KB |
Output is correct |
88 |
Correct |
109 ms |
26656 KB |
Output is correct |
89 |
Correct |
92 ms |
26780 KB |
Output is correct |
90 |
Correct |
96 ms |
26612 KB |
Output is correct |
91 |
Correct |
97 ms |
26736 KB |
Output is correct |
92 |
Correct |
104 ms |
27236 KB |
Output is correct |
93 |
Correct |
91 ms |
30044 KB |
Output is correct |
94 |
Correct |
94 ms |
32316 KB |
Output is correct |
95 |
Correct |
92 ms |
30696 KB |
Output is correct |
96 |
Correct |
97 ms |
28412 KB |
Output is correct |
97 |
Correct |
95 ms |
26600 KB |
Output is correct |
98 |
Correct |
99 ms |
26036 KB |
Output is correct |
99 |
Correct |
96 ms |
25828 KB |
Output is correct |
100 |
Correct |
96 ms |
29692 KB |
Output is correct |
101 |
Correct |
95 ms |
27252 KB |
Output is correct |
102 |
Correct |
92 ms |
26208 KB |
Output is correct |
103 |
Correct |
91 ms |
26048 KB |
Output is correct |
104 |
Correct |
65 ms |
26308 KB |
Output is correct |
105 |
Correct |
108 ms |
36756 KB |
Output is correct |
106 |
Correct |
73 ms |
26056 KB |
Output is correct |
107 |
Correct |
64 ms |
26272 KB |
Output is correct |
108 |
Correct |
102 ms |
41496 KB |
Output is correct |
109 |
Correct |
66 ms |
26308 KB |
Output is correct |
110 |
Correct |
118 ms |
42128 KB |
Output is correct |
111 |
Correct |
94 ms |
25408 KB |
Output is correct |