#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 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 |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6468 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 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 |
6488 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 |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 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 |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6468 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 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 |
6488 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 |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
3 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6560 KB |
Output is correct |
25 |
Correct |
3 ms |
6556 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6488 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6588 KB |
Output is correct |
30 |
Correct |
2 ms |
6488 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6488 KB |
Output is correct |
34 |
Correct |
2 ms |
6560 KB |
Output is correct |
35 |
Correct |
2 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6492 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 |
6488 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 |
2 ms |
6492 KB |
Output is correct |
48 |
Correct |
2 ms |
6720 KB |
Output is correct |
49 |
Correct |
2 ms |
6492 KB |
Output is correct |
50 |
Correct |
2 ms |
6492 KB |
Output is correct |
51 |
Correct |
2 ms |
6580 KB |
Output is correct |
52 |
Correct |
2 ms |
6488 KB |
Output is correct |
53 |
Correct |
2 ms |
6488 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6564 KB |
Output is correct |
56 |
Correct |
3 ms |
7004 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 |
6488 KB |
Output is correct |
60 |
Correct |
2 ms |
6492 KB |
Output is correct |
61 |
Correct |
2 ms |
6492 KB |
Output is correct |
62 |
Correct |
2 ms |
6748 KB |
Output is correct |
63 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 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 |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6468 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 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 |
6488 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 |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6492 KB |
Output is correct |
23 |
Correct |
3 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6560 KB |
Output is correct |
25 |
Correct |
3 ms |
6556 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6488 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6588 KB |
Output is correct |
30 |
Correct |
2 ms |
6488 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6488 KB |
Output is correct |
34 |
Correct |
2 ms |
6560 KB |
Output is correct |
35 |
Correct |
2 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
6492 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 |
6488 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 |
2 ms |
6492 KB |
Output is correct |
48 |
Correct |
2 ms |
6720 KB |
Output is correct |
49 |
Correct |
2 ms |
6492 KB |
Output is correct |
50 |
Correct |
2 ms |
6492 KB |
Output is correct |
51 |
Correct |
2 ms |
6580 KB |
Output is correct |
52 |
Correct |
2 ms |
6488 KB |
Output is correct |
53 |
Correct |
2 ms |
6488 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6564 KB |
Output is correct |
56 |
Correct |
3 ms |
7004 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 |
6488 KB |
Output is correct |
60 |
Correct |
2 ms |
6492 KB |
Output is correct |
61 |
Correct |
2 ms |
6492 KB |
Output is correct |
62 |
Correct |
2 ms |
6748 KB |
Output is correct |
63 |
Correct |
2 ms |
6492 KB |
Output is correct |
64 |
Correct |
83 ms |
16064 KB |
Output is correct |
65 |
Correct |
70 ms |
16076 KB |
Output is correct |
66 |
Correct |
62 ms |
15960 KB |
Output is correct |
67 |
Correct |
80 ms |
16016 KB |
Output is correct |
68 |
Correct |
61 ms |
16032 KB |
Output is correct |
69 |
Correct |
64 ms |
16024 KB |
Output is correct |
70 |
Correct |
74 ms |
15928 KB |
Output is correct |
71 |
Correct |
85 ms |
16028 KB |
Output is correct |
72 |
Correct |
60 ms |
15936 KB |
Output is correct |
73 |
Correct |
79 ms |
28560 KB |
Output is correct |
74 |
Correct |
77 ms |
28560 KB |
Output is correct |
75 |
Correct |
60 ms |
16020 KB |
Output is correct |
76 |
Correct |
65 ms |
15936 KB |
Output is correct |
77 |
Correct |
73 ms |
15924 KB |
Output is correct |
78 |
Correct |
73 ms |
15852 KB |
Output is correct |
79 |
Correct |
116 ms |
15936 KB |
Output is correct |
80 |
Correct |
74 ms |
15980 KB |
Output is correct |
81 |
Correct |
67 ms |
16356 KB |
Output is correct |
82 |
Correct |
59 ms |
16208 KB |
Output is correct |
83 |
Correct |
67 ms |
21144 KB |
Output is correct |
84 |
Correct |
69 ms |
23400 KB |
Output is correct |
85 |
Correct |
76 ms |
22856 KB |
Output is correct |
86 |
Correct |
60 ms |
17296 KB |
Output is correct |
87 |
Correct |
78 ms |
16732 KB |
Output is correct |
88 |
Correct |
61 ms |
17160 KB |
Output is correct |
89 |
Correct |
84 ms |
17308 KB |
Output is correct |
90 |
Correct |
66 ms |
16956 KB |
Output is correct |
91 |
Correct |
90 ms |
17320 KB |
Output is correct |
92 |
Correct |
93 ms |
17696 KB |
Output is correct |
93 |
Correct |
71 ms |
19988 KB |
Output is correct |
94 |
Correct |
67 ms |
21708 KB |
Output is correct |
95 |
Correct |
98 ms |
20480 KB |
Output is correct |
96 |
Correct |
74 ms |
18688 KB |
Output is correct |
97 |
Correct |
80 ms |
17052 KB |
Output is correct |
98 |
Correct |
95 ms |
16536 KB |
Output is correct |
99 |
Correct |
89 ms |
16468 KB |
Output is correct |
100 |
Correct |
94 ms |
19640 KB |
Output is correct |
101 |
Correct |
83 ms |
17540 KB |
Output is correct |
102 |
Correct |
113 ms |
16688 KB |
Output is correct |
103 |
Correct |
101 ms |
16588 KB |
Output is correct |
104 |
Correct |
48 ms |
16836 KB |
Output is correct |
105 |
Correct |
105 ms |
25508 KB |
Output is correct |
106 |
Correct |
46 ms |
16840 KB |
Output is correct |
107 |
Correct |
54 ms |
16684 KB |
Output is correct |
108 |
Correct |
108 ms |
29364 KB |
Output is correct |
109 |
Correct |
57 ms |
16928 KB |
Output is correct |
110 |
Correct |
133 ms |
29588 KB |
Output is correct |
111 |
Correct |
83 ms |
15932 KB |
Output is correct |