#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int NMAX = 2e5 + 1;
int dp[NMAX][3];
char c[NMAX];
vector <vector <int> > g(NMAX);
int ans = 0;
void dfs(int v, int e = -1){
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
}
int x = c[v] - '0';
int sum = -x;
int mx = 0;
for(int to : g[v]){
if(to == e) continue;
sum += max(dp[to][1], dp[to][0]);
mx = max(mx, dp[to][1]);
}
if(x) dp[v][1] = max(sum, 1), ans = max({ans, sum, 1, mx});
else dp[v][1] = sum, ans = max(ans, sum);
if(x){
int mx = 0;
int mx1 = 0;
int sum = 0;
for(int to : g[v]){
mx = max(mx, dp[to][2]);
mx1 = max(mx1, dp[to][1]);
}
dp[v][2] = max(mx1 + 1, mx - 1);
ans = max(ans, mx);
ans = max(ans, mx1 + 1);
}
else{
int mx = 0;
for(int to : g[v]){
mx = max(mx, dp[to][2]);
}
dp[v][2] = mx;
ans = max(ans, mx);
}
}
int main(){
int n; cin >> n;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i = 1; i <= n; i++)
cin >> c[i];
dfs(1);
cout << ans;
return 0;
}
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:37:13: warning: unused variable 'sum' [-Wunused-variable]
37 | int sum = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5084 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5032 KB |
Output is correct |
25 |
Correct |
4 ms |
5028 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5036 KB |
Output is correct |
28 |
Correct |
4 ms |
5088 KB |
Output is correct |
29 |
Correct |
4 ms |
5156 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5028 KB |
Output is correct |
32 |
Correct |
5 ms |
5024 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 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 |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5156 KB |
Output is correct |
40 |
Correct |
4 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5032 KB |
Output is correct |
47 |
Correct |
5 ms |
5076 KB |
Output is correct |
48 |
Correct |
6 ms |
5076 KB |
Output is correct |
49 |
Correct |
5 ms |
5088 KB |
Output is correct |
50 |
Correct |
5 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5084 KB |
Output is correct |
52 |
Correct |
4 ms |
5028 KB |
Output is correct |
53 |
Correct |
4 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5024 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
4 ms |
5028 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
6 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
5 ms |
5160 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
5 ms |
5276 KB |
Output is correct |
63 |
Correct |
4 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5084 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5032 KB |
Output is correct |
25 |
Correct |
4 ms |
5028 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5036 KB |
Output is correct |
28 |
Correct |
4 ms |
5088 KB |
Output is correct |
29 |
Correct |
4 ms |
5156 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5028 KB |
Output is correct |
32 |
Correct |
5 ms |
5024 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
4 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 |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5156 KB |
Output is correct |
40 |
Correct |
4 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5032 KB |
Output is correct |
47 |
Correct |
5 ms |
5076 KB |
Output is correct |
48 |
Correct |
6 ms |
5076 KB |
Output is correct |
49 |
Correct |
5 ms |
5088 KB |
Output is correct |
50 |
Correct |
5 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5084 KB |
Output is correct |
52 |
Correct |
4 ms |
5028 KB |
Output is correct |
53 |
Correct |
4 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5024 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
4 ms |
5028 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
6 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
5 ms |
5160 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
5 ms |
5276 KB |
Output is correct |
63 |
Correct |
4 ms |
5028 KB |
Output is correct |
64 |
Correct |
246 ms |
16532 KB |
Output is correct |
65 |
Correct |
226 ms |
16628 KB |
Output is correct |
66 |
Correct |
220 ms |
16732 KB |
Output is correct |
67 |
Correct |
220 ms |
16676 KB |
Output is correct |
68 |
Correct |
214 ms |
16624 KB |
Output is correct |
69 |
Correct |
209 ms |
16556 KB |
Output is correct |
70 |
Correct |
217 ms |
16692 KB |
Output is correct |
71 |
Correct |
215 ms |
16556 KB |
Output is correct |
72 |
Correct |
218 ms |
16592 KB |
Output is correct |
73 |
Correct |
238 ms |
26676 KB |
Output is correct |
74 |
Correct |
237 ms |
26532 KB |
Output is correct |
75 |
Correct |
202 ms |
16396 KB |
Output is correct |
76 |
Correct |
210 ms |
16676 KB |
Output is correct |
77 |
Correct |
209 ms |
16644 KB |
Output is correct |
78 |
Correct |
270 ms |
16528 KB |
Output is correct |
79 |
Correct |
219 ms |
16428 KB |
Output is correct |
80 |
Correct |
200 ms |
16472 KB |
Output is correct |
81 |
Correct |
188 ms |
16896 KB |
Output is correct |
82 |
Correct |
193 ms |
16844 KB |
Output is correct |
83 |
Correct |
262 ms |
20580 KB |
Output is correct |
84 |
Correct |
227 ms |
22356 KB |
Output is correct |
85 |
Correct |
229 ms |
22096 KB |
Output is correct |
86 |
Correct |
214 ms |
17684 KB |
Output is correct |
87 |
Correct |
227 ms |
17228 KB |
Output is correct |
88 |
Correct |
232 ms |
17816 KB |
Output is correct |
89 |
Correct |
201 ms |
17692 KB |
Output is correct |
90 |
Correct |
200 ms |
17480 KB |
Output is correct |
91 |
Correct |
201 ms |
17688 KB |
Output is correct |
92 |
Correct |
196 ms |
18008 KB |
Output is correct |
93 |
Correct |
234 ms |
19744 KB |
Output is correct |
94 |
Correct |
189 ms |
21036 KB |
Output is correct |
95 |
Correct |
203 ms |
20176 KB |
Output is correct |
96 |
Correct |
246 ms |
18676 KB |
Output is correct |
97 |
Correct |
190 ms |
17492 KB |
Output is correct |
98 |
Correct |
221 ms |
17164 KB |
Output is correct |
99 |
Correct |
215 ms |
16964 KB |
Output is correct |
100 |
Correct |
203 ms |
19468 KB |
Output is correct |
101 |
Correct |
249 ms |
17952 KB |
Output is correct |
102 |
Correct |
210 ms |
17516 KB |
Output is correct |
103 |
Correct |
226 ms |
17188 KB |
Output is correct |
104 |
Correct |
185 ms |
17320 KB |
Output is correct |
105 |
Correct |
224 ms |
24140 KB |
Output is correct |
106 |
Correct |
166 ms |
17140 KB |
Output is correct |
107 |
Correct |
210 ms |
17168 KB |
Output is correct |
108 |
Correct |
228 ms |
27256 KB |
Output is correct |
109 |
Correct |
162 ms |
17380 KB |
Output is correct |
110 |
Correct |
237 ms |
27564 KB |
Output is correct |
111 |
Correct |
200 ms |
16516 KB |
Output is correct |