#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
vector<int> g[N];
string s;
int dp[2][N];
void dfs(int u, int p) {
dp[0][u] = dp[1][u] = s[u];
int sum_dp = 0, mx_dp = 0;
for (int v : g[u]) {
if (v != p) {
dfs(v, u);
dp[0][u] = max(dp[0][u], dp[0][v]);
sum_dp += dp[1][v];
mx_dp = max(mx_dp, dp[1][v]);
}
}
dp[0][u] = max({dp[0][u], sum_dp - s[u], mx_dp + s[u]});
dp[1][u] = max(dp[1][u], sum_dp - s[u]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[--a].push_back(--b);
g[b].push_back(a);
}
cin >> s;
for (char &c : s) c -= '0';
dfs(0, 0);
cout << dp[0][0] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6516 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6368 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6548 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6504 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6516 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6368 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6548 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6504 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6544 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6540 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6560 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6812 KB |
Output is correct |
32 |
Correct |
3 ms |
6628 KB |
Output is correct |
33 |
Correct |
3 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6496 KB |
Output is correct |
35 |
Correct |
2 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6632 KB |
Output is correct |
37 |
Correct |
2 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
6492 KB |
Output is correct |
42 |
Correct |
2 ms |
6556 KB |
Output is correct |
43 |
Correct |
2 ms |
6492 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
2 ms |
6492 KB |
Output is correct |
46 |
Correct |
2 ms |
6492 KB |
Output is correct |
47 |
Correct |
3 ms |
6492 KB |
Output is correct |
48 |
Correct |
4 ms |
6492 KB |
Output is correct |
49 |
Correct |
3 ms |
6488 KB |
Output is correct |
50 |
Correct |
2 ms |
6492 KB |
Output is correct |
51 |
Correct |
2 ms |
6492 KB |
Output is correct |
52 |
Correct |
2 ms |
6492 KB |
Output is correct |
53 |
Correct |
2 ms |
6680 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6492 KB |
Output is correct |
56 |
Correct |
2 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6748 KB |
Output is correct |
58 |
Correct |
2 ms |
6492 KB |
Output is correct |
59 |
Correct |
2 ms |
6492 KB |
Output is correct |
60 |
Correct |
2 ms |
6492 KB |
Output is correct |
61 |
Correct |
2 ms |
6636 KB |
Output is correct |
62 |
Correct |
2 ms |
6492 KB |
Output is correct |
63 |
Correct |
3 ms |
6564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6516 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6368 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6548 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6504 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6544 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6540 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6560 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6812 KB |
Output is correct |
32 |
Correct |
3 ms |
6628 KB |
Output is correct |
33 |
Correct |
3 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6496 KB |
Output is correct |
35 |
Correct |
2 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6632 KB |
Output is correct |
37 |
Correct |
2 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
6492 KB |
Output is correct |
39 |
Correct |
2 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
6492 KB |
Output is correct |
42 |
Correct |
2 ms |
6556 KB |
Output is correct |
43 |
Correct |
2 ms |
6492 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
2 ms |
6492 KB |
Output is correct |
46 |
Correct |
2 ms |
6492 KB |
Output is correct |
47 |
Correct |
3 ms |
6492 KB |
Output is correct |
48 |
Correct |
4 ms |
6492 KB |
Output is correct |
49 |
Correct |
3 ms |
6488 KB |
Output is correct |
50 |
Correct |
2 ms |
6492 KB |
Output is correct |
51 |
Correct |
2 ms |
6492 KB |
Output is correct |
52 |
Correct |
2 ms |
6492 KB |
Output is correct |
53 |
Correct |
2 ms |
6680 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6492 KB |
Output is correct |
56 |
Correct |
2 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6748 KB |
Output is correct |
58 |
Correct |
2 ms |
6492 KB |
Output is correct |
59 |
Correct |
2 ms |
6492 KB |
Output is correct |
60 |
Correct |
2 ms |
6492 KB |
Output is correct |
61 |
Correct |
2 ms |
6636 KB |
Output is correct |
62 |
Correct |
2 ms |
6492 KB |
Output is correct |
63 |
Correct |
3 ms |
6564 KB |
Output is correct |
64 |
Correct |
59 ms |
16096 KB |
Output is correct |
65 |
Correct |
65 ms |
16016 KB |
Output is correct |
66 |
Correct |
59 ms |
16036 KB |
Output is correct |
67 |
Correct |
69 ms |
16088 KB |
Output is correct |
68 |
Correct |
110 ms |
15996 KB |
Output is correct |
69 |
Correct |
59 ms |
16012 KB |
Output is correct |
70 |
Correct |
56 ms |
16028 KB |
Output is correct |
71 |
Correct |
75 ms |
16032 KB |
Output is correct |
72 |
Correct |
61 ms |
16012 KB |
Output is correct |
73 |
Correct |
96 ms |
28548 KB |
Output is correct |
74 |
Correct |
87 ms |
28564 KB |
Output is correct |
75 |
Correct |
74 ms |
15924 KB |
Output is correct |
76 |
Correct |
58 ms |
15932 KB |
Output is correct |
77 |
Correct |
64 ms |
15920 KB |
Output is correct |
78 |
Correct |
64 ms |
15972 KB |
Output is correct |
79 |
Correct |
102 ms |
15932 KB |
Output is correct |
80 |
Correct |
60 ms |
15924 KB |
Output is correct |
81 |
Correct |
53 ms |
16328 KB |
Output is correct |
82 |
Correct |
58 ms |
16212 KB |
Output is correct |
83 |
Correct |
127 ms |
21104 KB |
Output is correct |
84 |
Correct |
71 ms |
23132 KB |
Output is correct |
85 |
Correct |
74 ms |
22944 KB |
Output is correct |
86 |
Correct |
72 ms |
17292 KB |
Output is correct |
87 |
Correct |
121 ms |
16708 KB |
Output is correct |
88 |
Correct |
68 ms |
17100 KB |
Output is correct |
89 |
Correct |
72 ms |
17268 KB |
Output is correct |
90 |
Correct |
74 ms |
17136 KB |
Output is correct |
91 |
Correct |
70 ms |
17268 KB |
Output is correct |
92 |
Correct |
64 ms |
17828 KB |
Output is correct |
93 |
Correct |
73 ms |
19892 KB |
Output is correct |
94 |
Correct |
65 ms |
21708 KB |
Output is correct |
95 |
Correct |
76 ms |
20300 KB |
Output is correct |
96 |
Correct |
85 ms |
18684 KB |
Output is correct |
97 |
Correct |
63 ms |
17132 KB |
Output is correct |
98 |
Correct |
80 ms |
16680 KB |
Output is correct |
99 |
Correct |
60 ms |
16348 KB |
Output is correct |
100 |
Correct |
68 ms |
19580 KB |
Output is correct |
101 |
Correct |
90 ms |
17420 KB |
Output is correct |
102 |
Correct |
85 ms |
16740 KB |
Output is correct |
103 |
Correct |
88 ms |
16564 KB |
Output is correct |
104 |
Correct |
42 ms |
16984 KB |
Output is correct |
105 |
Correct |
80 ms |
25492 KB |
Output is correct |
106 |
Correct |
47 ms |
16840 KB |
Output is correct |
107 |
Correct |
65 ms |
16836 KB |
Output is correct |
108 |
Correct |
75 ms |
29328 KB |
Output is correct |
109 |
Correct |
44 ms |
16836 KB |
Output is correct |
110 |
Correct |
101 ms |
29776 KB |
Output is correct |
111 |
Correct |
73 ms |
15856 KB |
Output is correct |