// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
vector<vector<int>> adj(N);
for(int i = 0; i < N-1; i++) {
int u, v; cin >> u >> v; --u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
string S; cin >> S;
vector<int> on(N); for(int i = 0; i < N; i++) on[i] = S[i] - '0';
vector<int> dp(N, 0);
function<void(int, int)> gen = [&](int u, int p) {
dp[u] = -(S[u] - '0');
for(auto v : adj[u]) if (v != p) {
gen(v, u);
dp[u] += max(dp[v], on[v]);
}
// cout << u << " " << max(dp[u], on[u]) << nl;
};
gen(0, -1);
int ans = 0;
function<void(int, int)> dfs = [&](int u, int p) {
// cout << u << " " << dp[u] << nl;
ans = max(ans, max(dp[u], on[u]));
for(auto v : adj[u]) if (v != p) {
ans = max(ans, max(dp[v], on[v]) + on[u]);
dp[u] -= max(dp[v], on[v]);
dp[v] += max(dp[u], on[u]);
dfs(v, u);
dp[v] -= max(dp[u], on[u]);
dp[u] += max(dp[v], on[v]);
}
};
dfs(0, -1);
cout << ans << nl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
456 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
412 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
464 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
420 KB |
Output is correct |
55 |
Correct |
1 ms |
456 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
596 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
456 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
412 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
464 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
420 KB |
Output is correct |
55 |
Correct |
1 ms |
456 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
596 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
155 ms |
15964 KB |
Output is correct |
65 |
Correct |
161 ms |
16004 KB |
Output is correct |
66 |
Correct |
155 ms |
16048 KB |
Output is correct |
67 |
Correct |
148 ms |
15940 KB |
Output is correct |
68 |
Correct |
148 ms |
15968 KB |
Output is correct |
69 |
Correct |
152 ms |
16020 KB |
Output is correct |
70 |
Correct |
147 ms |
15956 KB |
Output is correct |
71 |
Correct |
146 ms |
15884 KB |
Output is correct |
72 |
Correct |
153 ms |
15968 KB |
Output is correct |
73 |
Correct |
173 ms |
31128 KB |
Output is correct |
74 |
Correct |
167 ms |
31016 KB |
Output is correct |
75 |
Correct |
148 ms |
15868 KB |
Output is correct |
76 |
Correct |
140 ms |
15804 KB |
Output is correct |
77 |
Correct |
154 ms |
15812 KB |
Output is correct |
78 |
Correct |
151 ms |
15824 KB |
Output is correct |
79 |
Correct |
150 ms |
15888 KB |
Output is correct |
80 |
Correct |
148 ms |
15884 KB |
Output is correct |
81 |
Correct |
116 ms |
16316 KB |
Output is correct |
82 |
Correct |
130 ms |
15996 KB |
Output is correct |
83 |
Correct |
170 ms |
22096 KB |
Output is correct |
84 |
Correct |
161 ms |
24716 KB |
Output is correct |
85 |
Correct |
159 ms |
24212 KB |
Output is correct |
86 |
Correct |
147 ms |
17548 KB |
Output is correct |
87 |
Correct |
149 ms |
16872 KB |
Output is correct |
88 |
Correct |
134 ms |
17348 KB |
Output is correct |
89 |
Correct |
137 ms |
17264 KB |
Output is correct |
90 |
Correct |
139 ms |
17076 KB |
Output is correct |
91 |
Correct |
136 ms |
17324 KB |
Output is correct |
92 |
Correct |
141 ms |
17704 KB |
Output is correct |
93 |
Correct |
134 ms |
20572 KB |
Output is correct |
94 |
Correct |
136 ms |
22692 KB |
Output is correct |
95 |
Correct |
137 ms |
21180 KB |
Output is correct |
96 |
Correct |
152 ms |
19044 KB |
Output is correct |
97 |
Correct |
141 ms |
17136 KB |
Output is correct |
98 |
Correct |
138 ms |
16572 KB |
Output is correct |
99 |
Correct |
139 ms |
16340 KB |
Output is correct |
100 |
Correct |
146 ms |
20168 KB |
Output is correct |
101 |
Correct |
155 ms |
17672 KB |
Output is correct |
102 |
Correct |
140 ms |
16696 KB |
Output is correct |
103 |
Correct |
142 ms |
16460 KB |
Output is correct |
104 |
Correct |
82 ms |
16812 KB |
Output is correct |
105 |
Correct |
168 ms |
27324 KB |
Output is correct |
106 |
Correct |
74 ms |
16684 KB |
Output is correct |
107 |
Correct |
71 ms |
16684 KB |
Output is correct |
108 |
Correct |
176 ms |
31948 KB |
Output is correct |
109 |
Correct |
95 ms |
16892 KB |
Output is correct |
110 |
Correct |
176 ms |
32544 KB |
Output is correct |
111 |
Correct |
139 ms |
15776 KB |
Output is correct |