#include <bits/stdc++.h>
#define F first
#define S second
#define all(a) begin(a), end(a)
using namespace std;
int const N = 200005;
int n;
vector<int> adj[N];
string s;
void input() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> s;
}
int ans = 0;
int dp(int ver, int par) {
int ret = s[ver] == '1';
int suma = 0;
for (auto u : adj[ver]) if (u != par) {
int a = dp(u, ver);
if (a > 0) suma += a;
if (s[ver] == '1') {
ans = max(ans, a + 1);
}
}
ret = max(ret, suma - (s[ver] == '1'));
ans = max(ans, ret);
return ret;
}
void solve() {
dp(0, -1);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
input();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
4944 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4944 KB |
Output is correct |
19 |
Correct |
4 ms |
4960 KB |
Output is correct |
20 |
Correct |
4 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
4944 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4944 KB |
Output is correct |
19 |
Correct |
4 ms |
4960 KB |
Output is correct |
20 |
Correct |
4 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4944 KB |
Output is correct |
22 |
Correct |
4 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5024 KB |
Output is correct |
24 |
Correct |
5 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5024 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
4 ms |
5200 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
4 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5032 KB |
Output is correct |
36 |
Correct |
4 ms |
5028 KB |
Output is correct |
37 |
Correct |
4 ms |
5028 KB |
Output is correct |
38 |
Correct |
4 ms |
5072 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
3 ms |
5072 KB |
Output is correct |
44 |
Correct |
4 ms |
5072 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
3 ms |
5072 KB |
Output is correct |
47 |
Correct |
4 ms |
5072 KB |
Output is correct |
48 |
Correct |
4 ms |
5072 KB |
Output is correct |
49 |
Correct |
4 ms |
5080 KB |
Output is correct |
50 |
Correct |
4 ms |
5072 KB |
Output is correct |
51 |
Correct |
5 ms |
5072 KB |
Output is correct |
52 |
Correct |
4 ms |
5072 KB |
Output is correct |
53 |
Correct |
4 ms |
5028 KB |
Output is correct |
54 |
Correct |
4 ms |
5072 KB |
Output is correct |
55 |
Correct |
4 ms |
5072 KB |
Output is correct |
56 |
Correct |
4 ms |
5072 KB |
Output is correct |
57 |
Correct |
4 ms |
5200 KB |
Output is correct |
58 |
Correct |
4 ms |
5072 KB |
Output is correct |
59 |
Correct |
4 ms |
5028 KB |
Output is correct |
60 |
Correct |
4 ms |
5072 KB |
Output is correct |
61 |
Correct |
4 ms |
5072 KB |
Output is correct |
62 |
Correct |
3 ms |
5200 KB |
Output is correct |
63 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
4944 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4944 KB |
Output is correct |
19 |
Correct |
4 ms |
4960 KB |
Output is correct |
20 |
Correct |
4 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4944 KB |
Output is correct |
22 |
Correct |
4 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5024 KB |
Output is correct |
24 |
Correct |
5 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5024 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
4 ms |
5200 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
4 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5032 KB |
Output is correct |
36 |
Correct |
4 ms |
5028 KB |
Output is correct |
37 |
Correct |
4 ms |
5028 KB |
Output is correct |
38 |
Correct |
4 ms |
5072 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
3 ms |
5072 KB |
Output is correct |
44 |
Correct |
4 ms |
5072 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
3 ms |
5072 KB |
Output is correct |
47 |
Correct |
4 ms |
5072 KB |
Output is correct |
48 |
Correct |
4 ms |
5072 KB |
Output is correct |
49 |
Correct |
4 ms |
5080 KB |
Output is correct |
50 |
Correct |
4 ms |
5072 KB |
Output is correct |
51 |
Correct |
5 ms |
5072 KB |
Output is correct |
52 |
Correct |
4 ms |
5072 KB |
Output is correct |
53 |
Correct |
4 ms |
5028 KB |
Output is correct |
54 |
Correct |
4 ms |
5072 KB |
Output is correct |
55 |
Correct |
4 ms |
5072 KB |
Output is correct |
56 |
Correct |
4 ms |
5072 KB |
Output is correct |
57 |
Correct |
4 ms |
5200 KB |
Output is correct |
58 |
Correct |
4 ms |
5072 KB |
Output is correct |
59 |
Correct |
4 ms |
5028 KB |
Output is correct |
60 |
Correct |
4 ms |
5072 KB |
Output is correct |
61 |
Correct |
4 ms |
5072 KB |
Output is correct |
62 |
Correct |
3 ms |
5200 KB |
Output is correct |
63 |
Correct |
3 ms |
5032 KB |
Output is correct |
64 |
Correct |
180 ms |
14512 KB |
Output is correct |
65 |
Correct |
170 ms |
14496 KB |
Output is correct |
66 |
Correct |
136 ms |
14552 KB |
Output is correct |
67 |
Correct |
174 ms |
14508 KB |
Output is correct |
68 |
Correct |
152 ms |
14552 KB |
Output is correct |
69 |
Correct |
135 ms |
14432 KB |
Output is correct |
70 |
Correct |
171 ms |
14464 KB |
Output is correct |
71 |
Correct |
131 ms |
14440 KB |
Output is correct |
72 |
Correct |
165 ms |
14504 KB |
Output is correct |
73 |
Correct |
177 ms |
27128 KB |
Output is correct |
74 |
Correct |
187 ms |
26912 KB |
Output is correct |
75 |
Correct |
163 ms |
14436 KB |
Output is correct |
76 |
Correct |
165 ms |
14388 KB |
Output is correct |
77 |
Correct |
143 ms |
14432 KB |
Output is correct |
78 |
Correct |
168 ms |
14380 KB |
Output is correct |
79 |
Correct |
181 ms |
14320 KB |
Output is correct |
80 |
Correct |
182 ms |
14324 KB |
Output is correct |
81 |
Correct |
135 ms |
14752 KB |
Output is correct |
82 |
Correct |
148 ms |
14504 KB |
Output is correct |
83 |
Correct |
175 ms |
19596 KB |
Output is correct |
84 |
Correct |
159 ms |
21608 KB |
Output is correct |
85 |
Correct |
183 ms |
21228 KB |
Output is correct |
86 |
Correct |
169 ms |
15708 KB |
Output is correct |
87 |
Correct |
155 ms |
15236 KB |
Output is correct |
88 |
Correct |
162 ms |
15752 KB |
Output is correct |
89 |
Correct |
129 ms |
15636 KB |
Output is correct |
90 |
Correct |
160 ms |
15420 KB |
Output is correct |
91 |
Correct |
166 ms |
15632 KB |
Output is correct |
92 |
Correct |
152 ms |
15892 KB |
Output is correct |
93 |
Correct |
129 ms |
18352 KB |
Output is correct |
94 |
Correct |
142 ms |
20096 KB |
Output is correct |
95 |
Correct |
146 ms |
18748 KB |
Output is correct |
96 |
Correct |
126 ms |
17012 KB |
Output is correct |
97 |
Correct |
136 ms |
15364 KB |
Output is correct |
98 |
Correct |
131 ms |
14968 KB |
Output is correct |
99 |
Correct |
161 ms |
14944 KB |
Output is correct |
100 |
Correct |
128 ms |
17920 KB |
Output is correct |
101 |
Correct |
130 ms |
15856 KB |
Output is correct |
102 |
Correct |
135 ms |
15064 KB |
Output is correct |
103 |
Correct |
171 ms |
14960 KB |
Output is correct |
104 |
Correct |
87 ms |
15244 KB |
Output is correct |
105 |
Correct |
210 ms |
23816 KB |
Output is correct |
106 |
Correct |
87 ms |
15080 KB |
Output is correct |
107 |
Correct |
89 ms |
15164 KB |
Output is correct |
108 |
Correct |
199 ms |
27780 KB |
Output is correct |
109 |
Correct |
93 ms |
15192 KB |
Output is correct |
110 |
Correct |
177 ms |
28064 KB |
Output is correct |
111 |
Correct |
153 ms |
14392 KB |
Output is correct |