#include <iostream>
#include <vector>
#include <map>
using namespace std;
int n;
vector<vector<int>> g;
string s;
vector<map<int, int>> dp;
vector<int> dp_sum;
int f(int a, int b) {
return (s[a] == '1' ? max(1, dp_sum[a] - dp[a][b] - 1) : dp_sum[a] - dp[a][b]);
}
int dfs1(int u, int par) {
dp[u][par] = 0;
for (int v : g[u]) {
if (v != par) {
dp[u][v] = dfs1(v, u);
dp_sum[u] += dp[u][v];
}
}
return f(u, par);
}
void dfs2(int u, int par) {
dp[u][par] = f(par, u);
dp_sum[u] += dp[u][par];
for (int v : g[u]) {
if (v != par) dfs2(v, u);
}
}
int solve() {
dp.resize(n), dp_sum.resize(n, 0);
for (int v : g[0]) {
dp[0][v] += dfs1(v, 0);
dp_sum[0] += dp[0][v];
}
for (int v : g[0]) {
dfs2(v, 0);
}
int ans = 0;
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
ans = max(ans, f(u, v) + f(v, u));
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
g.resize(n);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> s;
cout << max(1, solve()) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
636 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
716 KB |
Output is correct |
32 |
Correct |
2 ms |
688 KB |
Output is correct |
33 |
Correct |
2 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
684 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
716 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
2 ms |
720 KB |
Output is correct |
47 |
Correct |
2 ms |
724 KB |
Output is correct |
48 |
Correct |
3 ms |
724 KB |
Output is correct |
49 |
Correct |
2 ms |
840 KB |
Output is correct |
50 |
Correct |
2 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
684 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
2 ms |
724 KB |
Output is correct |
54 |
Correct |
3 ms |
672 KB |
Output is correct |
55 |
Correct |
2 ms |
724 KB |
Output is correct |
56 |
Correct |
2 ms |
716 KB |
Output is correct |
57 |
Correct |
2 ms |
852 KB |
Output is correct |
58 |
Correct |
2 ms |
680 KB |
Output is correct |
59 |
Correct |
2 ms |
724 KB |
Output is correct |
60 |
Correct |
2 ms |
852 KB |
Output is correct |
61 |
Correct |
3 ms |
712 KB |
Output is correct |
62 |
Correct |
2 ms |
852 KB |
Output is correct |
63 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
636 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
716 KB |
Output is correct |
32 |
Correct |
2 ms |
688 KB |
Output is correct |
33 |
Correct |
2 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
684 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
716 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
2 ms |
720 KB |
Output is correct |
47 |
Correct |
2 ms |
724 KB |
Output is correct |
48 |
Correct |
3 ms |
724 KB |
Output is correct |
49 |
Correct |
2 ms |
840 KB |
Output is correct |
50 |
Correct |
2 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
684 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
2 ms |
724 KB |
Output is correct |
54 |
Correct |
3 ms |
672 KB |
Output is correct |
55 |
Correct |
2 ms |
724 KB |
Output is correct |
56 |
Correct |
2 ms |
716 KB |
Output is correct |
57 |
Correct |
2 ms |
852 KB |
Output is correct |
58 |
Correct |
2 ms |
680 KB |
Output is correct |
59 |
Correct |
2 ms |
724 KB |
Output is correct |
60 |
Correct |
2 ms |
852 KB |
Output is correct |
61 |
Correct |
3 ms |
712 KB |
Output is correct |
62 |
Correct |
2 ms |
852 KB |
Output is correct |
63 |
Correct |
3 ms |
724 KB |
Output is correct |
64 |
Correct |
311 ms |
43184 KB |
Output is correct |
65 |
Correct |
293 ms |
43280 KB |
Output is correct |
66 |
Correct |
339 ms |
43252 KB |
Output is correct |
67 |
Correct |
328 ms |
43272 KB |
Output is correct |
68 |
Correct |
317 ms |
43276 KB |
Output is correct |
69 |
Correct |
329 ms |
43268 KB |
Output is correct |
70 |
Correct |
300 ms |
43300 KB |
Output is correct |
71 |
Correct |
310 ms |
43268 KB |
Output is correct |
72 |
Correct |
305 ms |
43232 KB |
Output is correct |
73 |
Correct |
353 ms |
60852 KB |
Output is correct |
74 |
Correct |
385 ms |
60892 KB |
Output is correct |
75 |
Correct |
337 ms |
43092 KB |
Output is correct |
76 |
Correct |
312 ms |
43148 KB |
Output is correct |
77 |
Correct |
295 ms |
43144 KB |
Output is correct |
78 |
Correct |
321 ms |
43132 KB |
Output is correct |
79 |
Correct |
317 ms |
43052 KB |
Output is correct |
80 |
Correct |
322 ms |
43120 KB |
Output is correct |
81 |
Correct |
438 ms |
43516 KB |
Output is correct |
82 |
Correct |
346 ms |
43408 KB |
Output is correct |
83 |
Correct |
328 ms |
50400 KB |
Output is correct |
84 |
Correct |
317 ms |
53412 KB |
Output is correct |
85 |
Correct |
327 ms |
52960 KB |
Output is correct |
86 |
Correct |
318 ms |
45160 KB |
Output is correct |
87 |
Correct |
294 ms |
44328 KB |
Output is correct |
88 |
Correct |
378 ms |
44856 KB |
Output is correct |
89 |
Correct |
392 ms |
44788 KB |
Output is correct |
90 |
Correct |
351 ms |
44476 KB |
Output is correct |
91 |
Correct |
348 ms |
44716 KB |
Output is correct |
92 |
Correct |
367 ms |
45292 KB |
Output is correct |
93 |
Correct |
391 ms |
48732 KB |
Output is correct |
94 |
Correct |
390 ms |
51008 KB |
Output is correct |
95 |
Correct |
326 ms |
49268 KB |
Output is correct |
96 |
Correct |
357 ms |
46880 KB |
Output is correct |
97 |
Correct |
317 ms |
44592 KB |
Output is correct |
98 |
Correct |
297 ms |
43876 KB |
Output is correct |
99 |
Correct |
313 ms |
43612 KB |
Output is correct |
100 |
Correct |
379 ms |
48068 KB |
Output is correct |
101 |
Correct |
349 ms |
45260 KB |
Output is correct |
102 |
Correct |
332 ms |
44148 KB |
Output is correct |
103 |
Correct |
308 ms |
43872 KB |
Output is correct |
104 |
Correct |
548 ms |
44056 KB |
Output is correct |
105 |
Correct |
367 ms |
56596 KB |
Output is correct |
106 |
Correct |
523 ms |
43980 KB |
Output is correct |
107 |
Correct |
521 ms |
43856 KB |
Output is correct |
108 |
Correct |
343 ms |
62036 KB |
Output is correct |
109 |
Correct |
510 ms |
44096 KB |
Output is correct |
110 |
Correct |
355 ms |
62496 KB |
Output is correct |
111 |
Correct |
277 ms |
43124 KB |
Output is correct |