#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 200010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
vector<ll> adj[MAX];
ll ans = 0;
ll chk[MAX];
ll dp1[MAX];
ll dp2[MAX];
ll dp3[MAX];
void dfs1(ll x = 1, ll p = 0) {
if (chk[x]) dp1[x] = 1;
for (auto v : adj[x]) {
if (v == p) continue;
dfs1(v, x);
if (dp1[v]) dp1[x] = 1;
}
}
void dfs2(ll x = 1, ll p = 0) {
for (auto v : adj[x]) {
if (v == p) continue;
dfs2(v, x);
dp2[x] += dp2[v];
}
dp2[x] = max(dp1[x], max(dp2[x] - chk[x], chk[x]));
ans = max(ans, dp2[x]);
}
void dfs3(ll x = 1, ll p = 0) {
ll mx = 0;
ll mx2 = 0;
for (auto v : adj[x]) {
if (v == p) continue;
dfs3(v, x);
mx2 = max(mx2, dp2[v]);
mx = max(mx, dp3[v]);
}
dp3[x] = max(mx, mx2 + chk[x]);
ans = max(ans, dp3[x]);
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
ll N;
cin >> N;
ll i;
ll a, b;
for (i = 1; i < N; i++) cin >> a >> b, adj[a].push_back(b), adj[b].push_back(a);
string s;
cin >> s;
for (i = 0; i < N; i++) chk[i + 1] = s[i] - '0';
dfs1();
dfs2();
dfs3();
cout << ans << ln;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
2 ms |
4924 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
4 ms |
5040 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
2 ms |
4924 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
4 ms |
5040 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5040 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
4 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5160 KB |
Output is correct |
26 |
Correct |
5 ms |
5164 KB |
Output is correct |
27 |
Correct |
4 ms |
5184 KB |
Output is correct |
28 |
Correct |
3 ms |
5196 KB |
Output is correct |
29 |
Correct |
4 ms |
5324 KB |
Output is correct |
30 |
Correct |
5 ms |
5196 KB |
Output is correct |
31 |
Correct |
5 ms |
5168 KB |
Output is correct |
32 |
Correct |
4 ms |
5096 KB |
Output is correct |
33 |
Correct |
4 ms |
5160 KB |
Output is correct |
34 |
Correct |
4 ms |
5160 KB |
Output is correct |
35 |
Correct |
4 ms |
5092 KB |
Output is correct |
36 |
Correct |
4 ms |
5196 KB |
Output is correct |
37 |
Correct |
4 ms |
5196 KB |
Output is correct |
38 |
Correct |
4 ms |
5196 KB |
Output is correct |
39 |
Correct |
5 ms |
5196 KB |
Output is correct |
40 |
Correct |
5 ms |
5160 KB |
Output is correct |
41 |
Correct |
5 ms |
5196 KB |
Output is correct |
42 |
Correct |
4 ms |
5164 KB |
Output is correct |
43 |
Correct |
3 ms |
5196 KB |
Output is correct |
44 |
Correct |
3 ms |
5196 KB |
Output is correct |
45 |
Correct |
5 ms |
5160 KB |
Output is correct |
46 |
Correct |
4 ms |
5168 KB |
Output is correct |
47 |
Correct |
4 ms |
5220 KB |
Output is correct |
48 |
Correct |
3 ms |
5196 KB |
Output is correct |
49 |
Correct |
4 ms |
5216 KB |
Output is correct |
50 |
Correct |
4 ms |
5196 KB |
Output is correct |
51 |
Correct |
3 ms |
5196 KB |
Output is correct |
52 |
Correct |
4 ms |
5196 KB |
Output is correct |
53 |
Correct |
5 ms |
5160 KB |
Output is correct |
54 |
Correct |
4 ms |
5196 KB |
Output is correct |
55 |
Correct |
3 ms |
5196 KB |
Output is correct |
56 |
Correct |
4 ms |
5164 KB |
Output is correct |
57 |
Correct |
4 ms |
5292 KB |
Output is correct |
58 |
Correct |
6 ms |
5212 KB |
Output is correct |
59 |
Correct |
5 ms |
5208 KB |
Output is correct |
60 |
Correct |
4 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5064 KB |
Output is correct |
62 |
Correct |
6 ms |
5324 KB |
Output is correct |
63 |
Correct |
6 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
2 ms |
4924 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
4 ms |
5040 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5040 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
4 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5160 KB |
Output is correct |
26 |
Correct |
5 ms |
5164 KB |
Output is correct |
27 |
Correct |
4 ms |
5184 KB |
Output is correct |
28 |
Correct |
3 ms |
5196 KB |
Output is correct |
29 |
Correct |
4 ms |
5324 KB |
Output is correct |
30 |
Correct |
5 ms |
5196 KB |
Output is correct |
31 |
Correct |
5 ms |
5168 KB |
Output is correct |
32 |
Correct |
4 ms |
5096 KB |
Output is correct |
33 |
Correct |
4 ms |
5160 KB |
Output is correct |
34 |
Correct |
4 ms |
5160 KB |
Output is correct |
35 |
Correct |
4 ms |
5092 KB |
Output is correct |
36 |
Correct |
4 ms |
5196 KB |
Output is correct |
37 |
Correct |
4 ms |
5196 KB |
Output is correct |
38 |
Correct |
4 ms |
5196 KB |
Output is correct |
39 |
Correct |
5 ms |
5196 KB |
Output is correct |
40 |
Correct |
5 ms |
5160 KB |
Output is correct |
41 |
Correct |
5 ms |
5196 KB |
Output is correct |
42 |
Correct |
4 ms |
5164 KB |
Output is correct |
43 |
Correct |
3 ms |
5196 KB |
Output is correct |
44 |
Correct |
3 ms |
5196 KB |
Output is correct |
45 |
Correct |
5 ms |
5160 KB |
Output is correct |
46 |
Correct |
4 ms |
5168 KB |
Output is correct |
47 |
Correct |
4 ms |
5220 KB |
Output is correct |
48 |
Correct |
3 ms |
5196 KB |
Output is correct |
49 |
Correct |
4 ms |
5216 KB |
Output is correct |
50 |
Correct |
4 ms |
5196 KB |
Output is correct |
51 |
Correct |
3 ms |
5196 KB |
Output is correct |
52 |
Correct |
4 ms |
5196 KB |
Output is correct |
53 |
Correct |
5 ms |
5160 KB |
Output is correct |
54 |
Correct |
4 ms |
5196 KB |
Output is correct |
55 |
Correct |
3 ms |
5196 KB |
Output is correct |
56 |
Correct |
4 ms |
5164 KB |
Output is correct |
57 |
Correct |
4 ms |
5292 KB |
Output is correct |
58 |
Correct |
6 ms |
5212 KB |
Output is correct |
59 |
Correct |
5 ms |
5208 KB |
Output is correct |
60 |
Correct |
4 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5064 KB |
Output is correct |
62 |
Correct |
6 ms |
5324 KB |
Output is correct |
63 |
Correct |
6 ms |
5196 KB |
Output is correct |
64 |
Correct |
200 ms |
21536 KB |
Output is correct |
65 |
Correct |
206 ms |
21660 KB |
Output is correct |
66 |
Correct |
189 ms |
21636 KB |
Output is correct |
67 |
Correct |
198 ms |
21588 KB |
Output is correct |
68 |
Correct |
199 ms |
21724 KB |
Output is correct |
69 |
Correct |
208 ms |
21632 KB |
Output is correct |
70 |
Correct |
195 ms |
21608 KB |
Output is correct |
71 |
Correct |
207 ms |
21664 KB |
Output is correct |
72 |
Correct |
224 ms |
20620 KB |
Output is correct |
73 |
Correct |
252 ms |
35752 KB |
Output is correct |
74 |
Correct |
217 ms |
35652 KB |
Output is correct |
75 |
Correct |
196 ms |
22756 KB |
Output is correct |
76 |
Correct |
187 ms |
22800 KB |
Output is correct |
77 |
Correct |
212 ms |
21556 KB |
Output is correct |
78 |
Correct |
242 ms |
21612 KB |
Output is correct |
79 |
Correct |
225 ms |
21576 KB |
Output is correct |
80 |
Correct |
188 ms |
21624 KB |
Output is correct |
81 |
Correct |
153 ms |
21680 KB |
Output is correct |
82 |
Correct |
179 ms |
21640 KB |
Output is correct |
83 |
Correct |
217 ms |
27784 KB |
Output is correct |
84 |
Correct |
212 ms |
30256 KB |
Output is correct |
85 |
Correct |
198 ms |
29844 KB |
Output is correct |
86 |
Correct |
206 ms |
23164 KB |
Output is correct |
87 |
Correct |
209 ms |
22500 KB |
Output is correct |
88 |
Correct |
188 ms |
22640 KB |
Output is correct |
89 |
Correct |
190 ms |
22664 KB |
Output is correct |
90 |
Correct |
194 ms |
22324 KB |
Output is correct |
91 |
Correct |
188 ms |
22720 KB |
Output is correct |
92 |
Correct |
233 ms |
23028 KB |
Output is correct |
93 |
Correct |
177 ms |
25880 KB |
Output is correct |
94 |
Correct |
198 ms |
27864 KB |
Output is correct |
95 |
Correct |
161 ms |
26292 KB |
Output is correct |
96 |
Correct |
175 ms |
24176 KB |
Output is correct |
97 |
Correct |
190 ms |
22272 KB |
Output is correct |
98 |
Correct |
168 ms |
21812 KB |
Output is correct |
99 |
Correct |
220 ms |
21680 KB |
Output is correct |
100 |
Correct |
183 ms |
25252 KB |
Output is correct |
101 |
Correct |
210 ms |
22820 KB |
Output is correct |
102 |
Correct |
206 ms |
21884 KB |
Output is correct |
103 |
Correct |
192 ms |
21900 KB |
Output is correct |
104 |
Correct |
110 ms |
20688 KB |
Output is correct |
105 |
Correct |
221 ms |
32000 KB |
Output is correct |
106 |
Correct |
105 ms |
22128 KB |
Output is correct |
107 |
Correct |
98 ms |
22176 KB |
Output is correct |
108 |
Correct |
238 ms |
36796 KB |
Output is correct |
109 |
Correct |
95 ms |
20760 KB |
Output is correct |
110 |
Correct |
245 ms |
37132 KB |
Output is correct |
111 |
Correct |
185 ms |
21516 KB |
Output is correct |