/*
dp[i][0/1][0/1] = maximum answer for subtree of i with fixed (outside of subtree) & (subtree) existence of an activated generator
*/
#include <bits/stdc++.h>
using namespace std;
#define ar array
const int N = 2e5;
int n;
vector<int> adj[N];
string s;
int dp[N][2][2];
ar<int, 2> one(const ar<int, 2> one, const ar<int, 2> two) {
return {one[0] + two[0], max(one[1] + two[1], max(one[0] + two[1], one[1] + two[0]))};
}
ar<int, 2> two(const ar<int, 2> one, const ar<int, 2> two) {
return {one[0] + two[0], max(one[1] + two[0], one[0] + two[1])};
}
ar<int, 3> three(const ar<int, 3> one, const ar<int, 2> two) {
ar<int, 3> me{-n, -n, -n};
for (int a = 0; a < 3; a++) for (int b = 0; b < 2; b++) me[min(2, a + b)] = max(me[min(2, a + b)], one[a] + two[b]);
return me;
}
void dfs(int p, int i) {
for (int j : adj[i]) if (p != j) dfs(i, j);
ar<int, 3> f{0, -n, -n};
ar<int, 2> f2{0, -n};
int z = 0;
for (int j : adj[i]) if (p != j) {
z += dp[j][0][0];
f = three(f, ar<int, 2>{dp[j][1][0], dp[j][1][1]});
f2 = two(f2, ar<int, 2>{dp[j][1][0], dp[j][0][1]});
}
dp[i][1][0] = f[0];
dp[i][1][1] = max(f[1] - (s[i] == '1'), f[2] - (s[i] == '1'));
dp[i][0][0] = z;
dp[i][0][1] = max(f2[1], f[2] - (s[i] == '1'));
if (s[i] == '1') {
dp[i][0][1] = max(dp[i][0][1], max({f[0] + 1, f[1] + 1, f[2] - 1}));
dp[i][1][1] = max(dp[i][1][1], max({f[0] + 1, f[1] - 1, f[2] - 1}));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int h = 0; h < n - 1; h++) {
int i, j;
cin >> i >> j, i--, j--;
adj[i].push_back(j);
adj[j].push_back(i);
}
cin >> s;
dfs(-1, 0);
cout << max(dp[0][0][0], dp[0][0][1]) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
1 ms |
5980 KB |
Output is correct |
5 |
Correct |
1 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
6232 KB |
Output is correct |
7 |
Correct |
2 ms |
5988 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
1 ms |
6064 KB |
Output is correct |
13 |
Correct |
1 ms |
5824 KB |
Output is correct |
14 |
Correct |
2 ms |
5976 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
1 ms |
5980 KB |
Output is correct |
19 |
Correct |
1 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5976 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
1 ms |
5980 KB |
Output is correct |
5 |
Correct |
1 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
6232 KB |
Output is correct |
7 |
Correct |
2 ms |
5988 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
1 ms |
6064 KB |
Output is correct |
13 |
Correct |
1 ms |
5824 KB |
Output is correct |
14 |
Correct |
2 ms |
5976 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
1 ms |
5980 KB |
Output is correct |
19 |
Correct |
1 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5976 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5984 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5980 KB |
Output is correct |
27 |
Correct |
2 ms |
5980 KB |
Output is correct |
28 |
Correct |
2 ms |
6136 KB |
Output is correct |
29 |
Correct |
2 ms |
6232 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
5980 KB |
Output is correct |
32 |
Correct |
2 ms |
5980 KB |
Output is correct |
33 |
Correct |
2 ms |
5980 KB |
Output is correct |
34 |
Correct |
2 ms |
5980 KB |
Output is correct |
35 |
Correct |
3 ms |
5980 KB |
Output is correct |
36 |
Correct |
2 ms |
5980 KB |
Output is correct |
37 |
Correct |
2 ms |
5980 KB |
Output is correct |
38 |
Correct |
2 ms |
5980 KB |
Output is correct |
39 |
Correct |
2 ms |
6232 KB |
Output is correct |
40 |
Correct |
2 ms |
6236 KB |
Output is correct |
41 |
Correct |
2 ms |
6236 KB |
Output is correct |
42 |
Correct |
2 ms |
5980 KB |
Output is correct |
43 |
Correct |
2 ms |
5980 KB |
Output is correct |
44 |
Correct |
2 ms |
6076 KB |
Output is correct |
45 |
Correct |
2 ms |
5980 KB |
Output is correct |
46 |
Correct |
2 ms |
5980 KB |
Output is correct |
47 |
Correct |
2 ms |
6068 KB |
Output is correct |
48 |
Correct |
2 ms |
6232 KB |
Output is correct |
49 |
Correct |
3 ms |
6236 KB |
Output is correct |
50 |
Correct |
2 ms |
6148 KB |
Output is correct |
51 |
Correct |
2 ms |
5980 KB |
Output is correct |
52 |
Correct |
2 ms |
6072 KB |
Output is correct |
53 |
Correct |
2 ms |
5980 KB |
Output is correct |
54 |
Correct |
2 ms |
5980 KB |
Output is correct |
55 |
Correct |
2 ms |
5980 KB |
Output is correct |
56 |
Correct |
2 ms |
5980 KB |
Output is correct |
57 |
Correct |
2 ms |
6236 KB |
Output is correct |
58 |
Correct |
2 ms |
5980 KB |
Output is correct |
59 |
Correct |
2 ms |
5980 KB |
Output is correct |
60 |
Correct |
2 ms |
6236 KB |
Output is correct |
61 |
Correct |
2 ms |
5976 KB |
Output is correct |
62 |
Correct |
2 ms |
6324 KB |
Output is correct |
63 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
1 ms |
5980 KB |
Output is correct |
5 |
Correct |
1 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
6232 KB |
Output is correct |
7 |
Correct |
2 ms |
5988 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
1 ms |
6064 KB |
Output is correct |
13 |
Correct |
1 ms |
5824 KB |
Output is correct |
14 |
Correct |
2 ms |
5976 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
1 ms |
5980 KB |
Output is correct |
19 |
Correct |
1 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5976 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5984 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5980 KB |
Output is correct |
27 |
Correct |
2 ms |
5980 KB |
Output is correct |
28 |
Correct |
2 ms |
6136 KB |
Output is correct |
29 |
Correct |
2 ms |
6232 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
5980 KB |
Output is correct |
32 |
Correct |
2 ms |
5980 KB |
Output is correct |
33 |
Correct |
2 ms |
5980 KB |
Output is correct |
34 |
Correct |
2 ms |
5980 KB |
Output is correct |
35 |
Correct |
3 ms |
5980 KB |
Output is correct |
36 |
Correct |
2 ms |
5980 KB |
Output is correct |
37 |
Correct |
2 ms |
5980 KB |
Output is correct |
38 |
Correct |
2 ms |
5980 KB |
Output is correct |
39 |
Correct |
2 ms |
6232 KB |
Output is correct |
40 |
Correct |
2 ms |
6236 KB |
Output is correct |
41 |
Correct |
2 ms |
6236 KB |
Output is correct |
42 |
Correct |
2 ms |
5980 KB |
Output is correct |
43 |
Correct |
2 ms |
5980 KB |
Output is correct |
44 |
Correct |
2 ms |
6076 KB |
Output is correct |
45 |
Correct |
2 ms |
5980 KB |
Output is correct |
46 |
Correct |
2 ms |
5980 KB |
Output is correct |
47 |
Correct |
2 ms |
6068 KB |
Output is correct |
48 |
Correct |
2 ms |
6232 KB |
Output is correct |
49 |
Correct |
3 ms |
6236 KB |
Output is correct |
50 |
Correct |
2 ms |
6148 KB |
Output is correct |
51 |
Correct |
2 ms |
5980 KB |
Output is correct |
52 |
Correct |
2 ms |
6072 KB |
Output is correct |
53 |
Correct |
2 ms |
5980 KB |
Output is correct |
54 |
Correct |
2 ms |
5980 KB |
Output is correct |
55 |
Correct |
2 ms |
5980 KB |
Output is correct |
56 |
Correct |
2 ms |
5980 KB |
Output is correct |
57 |
Correct |
2 ms |
6236 KB |
Output is correct |
58 |
Correct |
2 ms |
5980 KB |
Output is correct |
59 |
Correct |
2 ms |
5980 KB |
Output is correct |
60 |
Correct |
2 ms |
6236 KB |
Output is correct |
61 |
Correct |
2 ms |
5976 KB |
Output is correct |
62 |
Correct |
2 ms |
6324 KB |
Output is correct |
63 |
Correct |
2 ms |
5980 KB |
Output is correct |
64 |
Correct |
111 ms |
17640 KB |
Output is correct |
65 |
Correct |
77 ms |
17556 KB |
Output is correct |
66 |
Correct |
74 ms |
17680 KB |
Output is correct |
67 |
Correct |
73 ms |
17548 KB |
Output is correct |
68 |
Correct |
71 ms |
17676 KB |
Output is correct |
69 |
Correct |
69 ms |
17548 KB |
Output is correct |
70 |
Correct |
75 ms |
17588 KB |
Output is correct |
71 |
Correct |
90 ms |
17564 KB |
Output is correct |
72 |
Correct |
87 ms |
17812 KB |
Output is correct |
73 |
Correct |
108 ms |
37804 KB |
Output is correct |
74 |
Correct |
127 ms |
37776 KB |
Output is correct |
75 |
Correct |
69 ms |
17372 KB |
Output is correct |
76 |
Correct |
89 ms |
17560 KB |
Output is correct |
77 |
Correct |
73 ms |
17552 KB |
Output is correct |
78 |
Correct |
77 ms |
17552 KB |
Output is correct |
79 |
Correct |
79 ms |
17464 KB |
Output is correct |
80 |
Correct |
72 ms |
17380 KB |
Output is correct |
81 |
Correct |
75 ms |
17868 KB |
Output is correct |
82 |
Correct |
110 ms |
17728 KB |
Output is correct |
83 |
Correct |
92 ms |
25748 KB |
Output is correct |
84 |
Correct |
111 ms |
29124 KB |
Output is correct |
85 |
Correct |
100 ms |
28676 KB |
Output is correct |
86 |
Correct |
78 ms |
19604 KB |
Output is correct |
87 |
Correct |
84 ms |
18908 KB |
Output is correct |
88 |
Correct |
74 ms |
19152 KB |
Output is correct |
89 |
Correct |
95 ms |
19300 KB |
Output is correct |
90 |
Correct |
89 ms |
19276 KB |
Output is correct |
91 |
Correct |
74 ms |
19340 KB |
Output is correct |
92 |
Correct |
76 ms |
19840 KB |
Output is correct |
93 |
Correct |
86 ms |
23636 KB |
Output is correct |
94 |
Correct |
81 ms |
26568 KB |
Output is correct |
95 |
Correct |
81 ms |
24304 KB |
Output is correct |
96 |
Correct |
82 ms |
21608 KB |
Output is correct |
97 |
Correct |
74 ms |
18944 KB |
Output is correct |
98 |
Correct |
72 ms |
18140 KB |
Output is correct |
99 |
Correct |
80 ms |
17868 KB |
Output is correct |
100 |
Correct |
83 ms |
22904 KB |
Output is correct |
101 |
Correct |
75 ms |
19728 KB |
Output is correct |
102 |
Correct |
90 ms |
18532 KB |
Output is correct |
103 |
Correct |
77 ms |
18068 KB |
Output is correct |
104 |
Correct |
48 ms |
18372 KB |
Output is correct |
105 |
Correct |
104 ms |
32656 KB |
Output is correct |
106 |
Correct |
56 ms |
18376 KB |
Output is correct |
107 |
Correct |
48 ms |
18372 KB |
Output is correct |
108 |
Correct |
115 ms |
39096 KB |
Output is correct |
109 |
Correct |
48 ms |
18328 KB |
Output is correct |
110 |
Correct |
131 ms |
39520 KB |
Output is correct |
111 |
Correct |
68 ms |
17568 KB |
Output is correct |