#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 2e5 + 8;
int n, a[N];
char s[N];
vector<int> g[N];
void upd(int &a, int b) { if (a < b) a = b; }
int dp[N], cnt[N][3], tmp[4];
int ans;
void dfs(int u, int p) {
for (int v : g[u]) if (v != p) {
dfs(v, u);
if (u == 2) {
int bug = 1;
}
fill(tmp, tmp + 4, 0);
for (int i = 0; i < 3; ++i)
upd(tmp[i + 1], cnt[u][i] + dp[v]);
upd(cnt[u][1], tmp[1]);
upd(cnt[u][2], tmp[2]);
upd(cnt[u][2], tmp[3]);
}
dp[u] = max({ a[u], cnt[u][1] - a[u], cnt[u][2] - a[u] });
ans = max({ ans, cnt[u][1] + a[u], cnt[u][2] - a[u] });
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> (s + 1);
for (int i = 1; i <= n; ++i) a[i] = s[i] ^ 48;
dfs(1, 0);
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:20:17: warning: unused variable 'bug' [-Wunused-variable]
20 | int bug = 1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4952 KB |
Output is correct |
4 |
Correct |
4 ms |
5040 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5044 KB |
Output is correct |
13 |
Correct |
4 ms |
5040 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4952 KB |
Output is correct |
4 |
Correct |
4 ms |
5040 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5044 KB |
Output is correct |
13 |
Correct |
4 ms |
5040 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5096 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5100 KB |
Output is correct |
27 |
Correct |
4 ms |
5180 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5096 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5176 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
4 ms |
5204 KB |
Output is correct |
41 |
Correct |
4 ms |
5180 KB |
Output is correct |
42 |
Correct |
4 ms |
5176 KB |
Output is correct |
43 |
Correct |
4 ms |
5100 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
3 ms |
5204 KB |
Output is correct |
46 |
Correct |
5 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5204 KB |
Output is correct |
48 |
Correct |
3 ms |
5232 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5176 KB |
Output is correct |
51 |
Correct |
4 ms |
5204 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
4 ms |
5176 KB |
Output is correct |
54 |
Correct |
3 ms |
5204 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5308 KB |
Output is correct |
58 |
Correct |
3 ms |
5052 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4952 KB |
Output is correct |
4 |
Correct |
4 ms |
5040 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5044 KB |
Output is correct |
13 |
Correct |
4 ms |
5040 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5096 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5100 KB |
Output is correct |
27 |
Correct |
4 ms |
5180 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5096 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5176 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
4 ms |
5204 KB |
Output is correct |
41 |
Correct |
4 ms |
5180 KB |
Output is correct |
42 |
Correct |
4 ms |
5176 KB |
Output is correct |
43 |
Correct |
4 ms |
5100 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
3 ms |
5204 KB |
Output is correct |
46 |
Correct |
5 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5204 KB |
Output is correct |
48 |
Correct |
3 ms |
5232 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5176 KB |
Output is correct |
51 |
Correct |
4 ms |
5204 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
4 ms |
5176 KB |
Output is correct |
54 |
Correct |
3 ms |
5204 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5308 KB |
Output is correct |
58 |
Correct |
3 ms |
5052 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5180 KB |
Output is correct |
64 |
Correct |
150 ms |
18220 KB |
Output is correct |
65 |
Correct |
147 ms |
18324 KB |
Output is correct |
66 |
Correct |
170 ms |
18220 KB |
Output is correct |
67 |
Correct |
146 ms |
18216 KB |
Output is correct |
68 |
Correct |
129 ms |
18212 KB |
Output is correct |
69 |
Correct |
142 ms |
18228 KB |
Output is correct |
70 |
Correct |
163 ms |
18252 KB |
Output is correct |
71 |
Correct |
134 ms |
18212 KB |
Output is correct |
72 |
Correct |
156 ms |
16468 KB |
Output is correct |
73 |
Correct |
176 ms |
33256 KB |
Output is correct |
74 |
Correct |
166 ms |
33228 KB |
Output is correct |
75 |
Correct |
172 ms |
17992 KB |
Output is correct |
76 |
Correct |
136 ms |
17988 KB |
Output is correct |
77 |
Correct |
136 ms |
18104 KB |
Output is correct |
78 |
Correct |
158 ms |
17996 KB |
Output is correct |
79 |
Correct |
141 ms |
18032 KB |
Output is correct |
80 |
Correct |
156 ms |
17992 KB |
Output is correct |
81 |
Correct |
134 ms |
18344 KB |
Output is correct |
82 |
Correct |
133 ms |
18248 KB |
Output is correct |
83 |
Correct |
162 ms |
24312 KB |
Output is correct |
84 |
Correct |
177 ms |
26920 KB |
Output is correct |
85 |
Correct |
155 ms |
26408 KB |
Output is correct |
86 |
Correct |
164 ms |
19788 KB |
Output is correct |
87 |
Correct |
152 ms |
19008 KB |
Output is correct |
88 |
Correct |
152 ms |
19532 KB |
Output is correct |
89 |
Correct |
139 ms |
19716 KB |
Output is correct |
90 |
Correct |
157 ms |
19336 KB |
Output is correct |
91 |
Correct |
162 ms |
19552 KB |
Output is correct |
92 |
Correct |
146 ms |
20052 KB |
Output is correct |
93 |
Correct |
149 ms |
22832 KB |
Output is correct |
94 |
Correct |
116 ms |
24888 KB |
Output is correct |
95 |
Correct |
153 ms |
23412 KB |
Output is correct |
96 |
Correct |
174 ms |
21164 KB |
Output is correct |
97 |
Correct |
141 ms |
19460 KB |
Output is correct |
98 |
Correct |
140 ms |
18760 KB |
Output is correct |
99 |
Correct |
150 ms |
18508 KB |
Output is correct |
100 |
Correct |
144 ms |
22472 KB |
Output is correct |
101 |
Correct |
156 ms |
19884 KB |
Output is correct |
102 |
Correct |
156 ms |
19032 KB |
Output is correct |
103 |
Correct |
141 ms |
18764 KB |
Output is correct |
104 |
Correct |
73 ms |
16576 KB |
Output is correct |
105 |
Correct |
167 ms |
29540 KB |
Output is correct |
106 |
Correct |
74 ms |
16448 KB |
Output is correct |
107 |
Correct |
91 ms |
16448 KB |
Output is correct |
108 |
Correct |
156 ms |
34368 KB |
Output is correct |
109 |
Correct |
103 ms |
16576 KB |
Output is correct |
110 |
Correct |
172 ms |
34808 KB |
Output is correct |
111 |
Correct |
132 ms |
17996 KB |
Output is correct |