#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 5e5 + 23;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define sz(x) ((int)x.size())
#define kill(x) cout<<x , exit(0);
#define all(x) x.begin(),x.end()
#define lc (v<<1)
#define rc ((v<<1)|1)
#define debug(x) cerr<< #x << " : " << x << '\n';
#define int ll
int n;
int dp[N][2];
vector<int> G[N];
bool is[N];
void dfs(int v,int p=-1) {
int sum0 = 0,sum1 = 0,mx1=0,mx0=0;
for(int u : G[v]) if(u != p) {
dfs(u,v);
sum0 += dp[u][0];
sum1 += dp[u][1];
mx1= max(mx1,dp[u][1]);
mx0=max(mx0,dp[u][0]);
}
if(is[v]) {
dp[v][0] = max({mx0,sum1- 1,1 + mx1});
} else {
dp[v][0] = max({mx0,sum1});
}
if(is[v]) {
dp[v][1] = max({1LL,sum1-1});
} else {
dp[v][1] = sum1;
}
}
int32_t main() {
cin>>n;
for(int i = 1; i< n ; i++) {
int v,u; cin>>v>>u;
G[v].pb(u);
G[u].pb(v);
}
for(int i = 1; i <= n ; i++) {
char c; cin>>c;
is[i] = (c == '1');
}
int ans =0;
dfs(1);
cout<<dp[1][0] << '\n';
return 0;
}
Compilation message
power.cpp: In function 'int32_t main()':
power.cpp:61:6: warning: unused variable 'ans' [-Wunused-variable]
61 | int ans =0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
4 ms |
12752 KB |
Output is correct |
5 |
Correct |
4 ms |
12636 KB |
Output is correct |
6 |
Correct |
4 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
5 ms |
13140 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12636 KB |
Output is correct |
12 |
Correct |
4 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12632 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
4 ms |
12692 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
4 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
4 ms |
12632 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
4 ms |
12752 KB |
Output is correct |
5 |
Correct |
4 ms |
12636 KB |
Output is correct |
6 |
Correct |
4 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
5 ms |
13140 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12636 KB |
Output is correct |
12 |
Correct |
4 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12632 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
4 ms |
12692 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
4 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
4 ms |
12632 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
22 |
Correct |
4 ms |
12636 KB |
Output is correct |
23 |
Correct |
5 ms |
12636 KB |
Output is correct |
24 |
Correct |
6 ms |
12636 KB |
Output is correct |
25 |
Correct |
4 ms |
12636 KB |
Output is correct |
26 |
Correct |
5 ms |
12636 KB |
Output is correct |
27 |
Correct |
6 ms |
12740 KB |
Output is correct |
28 |
Correct |
4 ms |
12636 KB |
Output is correct |
29 |
Correct |
7 ms |
13144 KB |
Output is correct |
30 |
Correct |
5 ms |
12888 KB |
Output is correct |
31 |
Correct |
4 ms |
12636 KB |
Output is correct |
32 |
Correct |
4 ms |
12636 KB |
Output is correct |
33 |
Correct |
4 ms |
12636 KB |
Output is correct |
34 |
Correct |
5 ms |
12892 KB |
Output is correct |
35 |
Correct |
5 ms |
12724 KB |
Output is correct |
36 |
Correct |
5 ms |
12636 KB |
Output is correct |
37 |
Correct |
4 ms |
12636 KB |
Output is correct |
38 |
Correct |
5 ms |
12636 KB |
Output is correct |
39 |
Correct |
5 ms |
12892 KB |
Output is correct |
40 |
Correct |
4 ms |
12892 KB |
Output is correct |
41 |
Correct |
4 ms |
12892 KB |
Output is correct |
42 |
Correct |
4 ms |
12636 KB |
Output is correct |
43 |
Correct |
6 ms |
12632 KB |
Output is correct |
44 |
Correct |
5 ms |
12892 KB |
Output is correct |
45 |
Correct |
4 ms |
12636 KB |
Output is correct |
46 |
Correct |
5 ms |
12704 KB |
Output is correct |
47 |
Correct |
5 ms |
12772 KB |
Output is correct |
48 |
Correct |
4 ms |
12892 KB |
Output is correct |
49 |
Correct |
4 ms |
12892 KB |
Output is correct |
50 |
Correct |
5 ms |
12772 KB |
Output is correct |
51 |
Correct |
4 ms |
12888 KB |
Output is correct |
52 |
Correct |
4 ms |
12636 KB |
Output is correct |
53 |
Correct |
5 ms |
12632 KB |
Output is correct |
54 |
Correct |
4 ms |
12636 KB |
Output is correct |
55 |
Correct |
4 ms |
12724 KB |
Output is correct |
56 |
Correct |
5 ms |
12636 KB |
Output is correct |
57 |
Correct |
6 ms |
12996 KB |
Output is correct |
58 |
Correct |
4 ms |
12636 KB |
Output is correct |
59 |
Correct |
5 ms |
12636 KB |
Output is correct |
60 |
Correct |
6 ms |
12772 KB |
Output is correct |
61 |
Correct |
4 ms |
12636 KB |
Output is correct |
62 |
Correct |
4 ms |
12736 KB |
Output is correct |
63 |
Correct |
6 ms |
12632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
4 ms |
12752 KB |
Output is correct |
5 |
Correct |
4 ms |
12636 KB |
Output is correct |
6 |
Correct |
4 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
5 ms |
13140 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12636 KB |
Output is correct |
12 |
Correct |
4 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12632 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
4 ms |
12692 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
4 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
4 ms |
12632 KB |
Output is correct |
21 |
Correct |
4 ms |
12636 KB |
Output is correct |
22 |
Correct |
4 ms |
12636 KB |
Output is correct |
23 |
Correct |
5 ms |
12636 KB |
Output is correct |
24 |
Correct |
6 ms |
12636 KB |
Output is correct |
25 |
Correct |
4 ms |
12636 KB |
Output is correct |
26 |
Correct |
5 ms |
12636 KB |
Output is correct |
27 |
Correct |
6 ms |
12740 KB |
Output is correct |
28 |
Correct |
4 ms |
12636 KB |
Output is correct |
29 |
Correct |
7 ms |
13144 KB |
Output is correct |
30 |
Correct |
5 ms |
12888 KB |
Output is correct |
31 |
Correct |
4 ms |
12636 KB |
Output is correct |
32 |
Correct |
4 ms |
12636 KB |
Output is correct |
33 |
Correct |
4 ms |
12636 KB |
Output is correct |
34 |
Correct |
5 ms |
12892 KB |
Output is correct |
35 |
Correct |
5 ms |
12724 KB |
Output is correct |
36 |
Correct |
5 ms |
12636 KB |
Output is correct |
37 |
Correct |
4 ms |
12636 KB |
Output is correct |
38 |
Correct |
5 ms |
12636 KB |
Output is correct |
39 |
Correct |
5 ms |
12892 KB |
Output is correct |
40 |
Correct |
4 ms |
12892 KB |
Output is correct |
41 |
Correct |
4 ms |
12892 KB |
Output is correct |
42 |
Correct |
4 ms |
12636 KB |
Output is correct |
43 |
Correct |
6 ms |
12632 KB |
Output is correct |
44 |
Correct |
5 ms |
12892 KB |
Output is correct |
45 |
Correct |
4 ms |
12636 KB |
Output is correct |
46 |
Correct |
5 ms |
12704 KB |
Output is correct |
47 |
Correct |
5 ms |
12772 KB |
Output is correct |
48 |
Correct |
4 ms |
12892 KB |
Output is correct |
49 |
Correct |
4 ms |
12892 KB |
Output is correct |
50 |
Correct |
5 ms |
12772 KB |
Output is correct |
51 |
Correct |
4 ms |
12888 KB |
Output is correct |
52 |
Correct |
4 ms |
12636 KB |
Output is correct |
53 |
Correct |
5 ms |
12632 KB |
Output is correct |
54 |
Correct |
4 ms |
12636 KB |
Output is correct |
55 |
Correct |
4 ms |
12724 KB |
Output is correct |
56 |
Correct |
5 ms |
12636 KB |
Output is correct |
57 |
Correct |
6 ms |
12996 KB |
Output is correct |
58 |
Correct |
4 ms |
12636 KB |
Output is correct |
59 |
Correct |
5 ms |
12636 KB |
Output is correct |
60 |
Correct |
6 ms |
12772 KB |
Output is correct |
61 |
Correct |
4 ms |
12636 KB |
Output is correct |
62 |
Correct |
4 ms |
12736 KB |
Output is correct |
63 |
Correct |
6 ms |
12632 KB |
Output is correct |
64 |
Correct |
187 ms |
27540 KB |
Output is correct |
65 |
Correct |
175 ms |
26964 KB |
Output is correct |
66 |
Correct |
180 ms |
27108 KB |
Output is correct |
67 |
Correct |
172 ms |
26960 KB |
Output is correct |
68 |
Correct |
173 ms |
26996 KB |
Output is correct |
69 |
Correct |
148 ms |
27044 KB |
Output is correct |
70 |
Correct |
145 ms |
27048 KB |
Output is correct |
71 |
Correct |
157 ms |
27276 KB |
Output is correct |
72 |
Correct |
156 ms |
27096 KB |
Output is correct |
73 |
Correct |
216 ms |
41272 KB |
Output is correct |
74 |
Correct |
187 ms |
41016 KB |
Output is correct |
75 |
Correct |
170 ms |
28244 KB |
Output is correct |
76 |
Correct |
201 ms |
28240 KB |
Output is correct |
77 |
Correct |
190 ms |
26976 KB |
Output is correct |
78 |
Correct |
150 ms |
26888 KB |
Output is correct |
79 |
Correct |
216 ms |
27024 KB |
Output is correct |
80 |
Correct |
154 ms |
27132 KB |
Output is correct |
81 |
Correct |
132 ms |
26924 KB |
Output is correct |
82 |
Correct |
173 ms |
26968 KB |
Output is correct |
83 |
Correct |
185 ms |
33096 KB |
Output is correct |
84 |
Correct |
222 ms |
35668 KB |
Output is correct |
85 |
Correct |
193 ms |
35320 KB |
Output is correct |
86 |
Correct |
184 ms |
28496 KB |
Output is correct |
87 |
Correct |
182 ms |
27872 KB |
Output is correct |
88 |
Correct |
189 ms |
28292 KB |
Output is correct |
89 |
Correct |
139 ms |
27888 KB |
Output is correct |
90 |
Correct |
154 ms |
27692 KB |
Output is correct |
91 |
Correct |
164 ms |
28232 KB |
Output is correct |
92 |
Correct |
166 ms |
28496 KB |
Output is correct |
93 |
Correct |
148 ms |
31124 KB |
Output is correct |
94 |
Correct |
162 ms |
32968 KB |
Output is correct |
95 |
Correct |
170 ms |
31680 KB |
Output is correct |
96 |
Correct |
147 ms |
29520 KB |
Output is correct |
97 |
Correct |
167 ms |
27724 KB |
Output is correct |
98 |
Correct |
167 ms |
27184 KB |
Output is correct |
99 |
Correct |
141 ms |
27076 KB |
Output is correct |
100 |
Correct |
159 ms |
30588 KB |
Output is correct |
101 |
Correct |
216 ms |
28092 KB |
Output is correct |
102 |
Correct |
161 ms |
27344 KB |
Output is correct |
103 |
Correct |
170 ms |
27388 KB |
Output is correct |
104 |
Correct |
135 ms |
28024 KB |
Output is correct |
105 |
Correct |
207 ms |
37440 KB |
Output is correct |
106 |
Correct |
121 ms |
27492 KB |
Output is correct |
107 |
Correct |
128 ms |
27556 KB |
Output is correct |
108 |
Correct |
204 ms |
41992 KB |
Output is correct |
109 |
Correct |
130 ms |
27476 KB |
Output is correct |
110 |
Correct |
187 ms |
42556 KB |
Output is correct |
111 |
Correct |
179 ms |
27076 KB |
Output is correct |