#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
#define pb push_back
#define popb pop_back
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
const int nax = 2e5 + 4;
int n;
string str;
int C[nax];
vi adj[nax];
int dp[nax][2][2];
int hw[nax], par[nax];
void dfs(int x, int p)
{
par[x] = p;
hw[x] = C[x];
for(auto e: adj[x])
{
if(e != p)
{
dfs(e,x );
hw[x] += hw[e];
}
}
}
int f(int x, int ext, int b)
{
if(dp[x][ext][b] != -1)
return dp[x][ext][b];
if(b && ext)
return dp[x][ext][b] = 1;
if(ext)
{
int rep = 0;
for(auto e: adj[x])
{
if(e != par[x] && hw[e] > 0 )
{
int p = C[e] ? f(e, 1, 1): 0;
int q = f(e, 1, 0);
rep += max(p, q);
}
}
return dp[x][ext][b] = max(0, rep - C[x]);
}
else
{
if(b)
{
int u= 0;
for(auto e: adj[x])
{
if(e != par[x] && hw[e] > 0)
{
int p = C[e] ? f(e, 1, 1): 0;
int q = f(e, 1, 0);
u = max(u, max(p, q));
}
}
return dp[x][ext][b] = u + 1;
}
else
{
int sum = 0;
int maxi = 0;
for(auto e: adj[x])
{
if(e != par[x] && hw[e] > 0 )
{
int p = C[e] ? f(e, 1, 1): 0;
int q= f(e, 1, 0);
int pp = C[e] ? f(e, 0, 1): 0 ;
int qp = f(e, 0, 0);
maxi = max(maxi, max(pp, qp));
sum += max(p, q);
}
}
return dp[x][ext][b] = max(maxi, sum - C[x]);
}
}
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
memset(dp, -1,sizeof(dp));
cin >> n;
for(int i = 1; i < n;i ++)
{
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
cin >> str;
for(int i = 1; i <= n ;i ++)
C[i] = (str[i - 1] == '1');
dfs(1, 0);
int p = C[1] ? f(1, 0, 1): 0 ;
int q = f(1, 0, 0);
cout << max(p, q);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8068 KB |
Output is correct |
3 |
Correct |
5 ms |
8276 KB |
Output is correct |
4 |
Correct |
5 ms |
8160 KB |
Output is correct |
5 |
Correct |
4 ms |
8124 KB |
Output is correct |
6 |
Correct |
5 ms |
8168 KB |
Output is correct |
7 |
Correct |
4 ms |
8164 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8276 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
3 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8164 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8156 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8164 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8068 KB |
Output is correct |
3 |
Correct |
5 ms |
8276 KB |
Output is correct |
4 |
Correct |
5 ms |
8160 KB |
Output is correct |
5 |
Correct |
4 ms |
8124 KB |
Output is correct |
6 |
Correct |
5 ms |
8168 KB |
Output is correct |
7 |
Correct |
4 ms |
8164 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8276 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
3 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8164 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8156 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8164 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
5 ms |
8220 KB |
Output is correct |
23 |
Correct |
5 ms |
8276 KB |
Output is correct |
24 |
Correct |
4 ms |
8276 KB |
Output is correct |
25 |
Correct |
5 ms |
8180 KB |
Output is correct |
26 |
Correct |
6 ms |
8404 KB |
Output is correct |
27 |
Correct |
5 ms |
8276 KB |
Output is correct |
28 |
Correct |
5 ms |
8276 KB |
Output is correct |
29 |
Correct |
5 ms |
8420 KB |
Output is correct |
30 |
Correct |
5 ms |
8312 KB |
Output is correct |
31 |
Correct |
5 ms |
8224 KB |
Output is correct |
32 |
Correct |
5 ms |
8276 KB |
Output is correct |
33 |
Correct |
6 ms |
8276 KB |
Output is correct |
34 |
Correct |
5 ms |
8276 KB |
Output is correct |
35 |
Correct |
5 ms |
8148 KB |
Output is correct |
36 |
Correct |
7 ms |
8192 KB |
Output is correct |
37 |
Correct |
5 ms |
8276 KB |
Output is correct |
38 |
Correct |
5 ms |
8276 KB |
Output is correct |
39 |
Correct |
5 ms |
8296 KB |
Output is correct |
40 |
Correct |
5 ms |
8304 KB |
Output is correct |
41 |
Correct |
5 ms |
8276 KB |
Output is correct |
42 |
Correct |
5 ms |
8276 KB |
Output is correct |
43 |
Correct |
6 ms |
8404 KB |
Output is correct |
44 |
Correct |
5 ms |
8228 KB |
Output is correct |
45 |
Correct |
5 ms |
8276 KB |
Output is correct |
46 |
Correct |
4 ms |
8300 KB |
Output is correct |
47 |
Correct |
5 ms |
8276 KB |
Output is correct |
48 |
Correct |
5 ms |
8300 KB |
Output is correct |
49 |
Correct |
5 ms |
8356 KB |
Output is correct |
50 |
Correct |
5 ms |
8304 KB |
Output is correct |
51 |
Correct |
5 ms |
8404 KB |
Output is correct |
52 |
Correct |
5 ms |
8172 KB |
Output is correct |
53 |
Correct |
5 ms |
8276 KB |
Output is correct |
54 |
Correct |
5 ms |
8220 KB |
Output is correct |
55 |
Correct |
5 ms |
8300 KB |
Output is correct |
56 |
Correct |
5 ms |
8276 KB |
Output is correct |
57 |
Correct |
5 ms |
8404 KB |
Output is correct |
58 |
Correct |
6 ms |
8276 KB |
Output is correct |
59 |
Correct |
5 ms |
8176 KB |
Output is correct |
60 |
Correct |
5 ms |
8276 KB |
Output is correct |
61 |
Correct |
5 ms |
8276 KB |
Output is correct |
62 |
Correct |
5 ms |
8404 KB |
Output is correct |
63 |
Correct |
5 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8068 KB |
Output is correct |
3 |
Correct |
5 ms |
8276 KB |
Output is correct |
4 |
Correct |
5 ms |
8160 KB |
Output is correct |
5 |
Correct |
4 ms |
8124 KB |
Output is correct |
6 |
Correct |
5 ms |
8168 KB |
Output is correct |
7 |
Correct |
4 ms |
8164 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8276 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
3 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8164 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8156 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8164 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
5 ms |
8220 KB |
Output is correct |
23 |
Correct |
5 ms |
8276 KB |
Output is correct |
24 |
Correct |
4 ms |
8276 KB |
Output is correct |
25 |
Correct |
5 ms |
8180 KB |
Output is correct |
26 |
Correct |
6 ms |
8404 KB |
Output is correct |
27 |
Correct |
5 ms |
8276 KB |
Output is correct |
28 |
Correct |
5 ms |
8276 KB |
Output is correct |
29 |
Correct |
5 ms |
8420 KB |
Output is correct |
30 |
Correct |
5 ms |
8312 KB |
Output is correct |
31 |
Correct |
5 ms |
8224 KB |
Output is correct |
32 |
Correct |
5 ms |
8276 KB |
Output is correct |
33 |
Correct |
6 ms |
8276 KB |
Output is correct |
34 |
Correct |
5 ms |
8276 KB |
Output is correct |
35 |
Correct |
5 ms |
8148 KB |
Output is correct |
36 |
Correct |
7 ms |
8192 KB |
Output is correct |
37 |
Correct |
5 ms |
8276 KB |
Output is correct |
38 |
Correct |
5 ms |
8276 KB |
Output is correct |
39 |
Correct |
5 ms |
8296 KB |
Output is correct |
40 |
Correct |
5 ms |
8304 KB |
Output is correct |
41 |
Correct |
5 ms |
8276 KB |
Output is correct |
42 |
Correct |
5 ms |
8276 KB |
Output is correct |
43 |
Correct |
6 ms |
8404 KB |
Output is correct |
44 |
Correct |
5 ms |
8228 KB |
Output is correct |
45 |
Correct |
5 ms |
8276 KB |
Output is correct |
46 |
Correct |
4 ms |
8300 KB |
Output is correct |
47 |
Correct |
5 ms |
8276 KB |
Output is correct |
48 |
Correct |
5 ms |
8300 KB |
Output is correct |
49 |
Correct |
5 ms |
8356 KB |
Output is correct |
50 |
Correct |
5 ms |
8304 KB |
Output is correct |
51 |
Correct |
5 ms |
8404 KB |
Output is correct |
52 |
Correct |
5 ms |
8172 KB |
Output is correct |
53 |
Correct |
5 ms |
8276 KB |
Output is correct |
54 |
Correct |
5 ms |
8220 KB |
Output is correct |
55 |
Correct |
5 ms |
8300 KB |
Output is correct |
56 |
Correct |
5 ms |
8276 KB |
Output is correct |
57 |
Correct |
5 ms |
8404 KB |
Output is correct |
58 |
Correct |
6 ms |
8276 KB |
Output is correct |
59 |
Correct |
5 ms |
8176 KB |
Output is correct |
60 |
Correct |
5 ms |
8276 KB |
Output is correct |
61 |
Correct |
5 ms |
8276 KB |
Output is correct |
62 |
Correct |
5 ms |
8404 KB |
Output is correct |
63 |
Correct |
5 ms |
8276 KB |
Output is correct |
64 |
Correct |
216 ms |
19948 KB |
Output is correct |
65 |
Correct |
181 ms |
19852 KB |
Output is correct |
66 |
Correct |
205 ms |
19916 KB |
Output is correct |
67 |
Correct |
175 ms |
19924 KB |
Output is correct |
68 |
Correct |
178 ms |
19868 KB |
Output is correct |
69 |
Correct |
154 ms |
19880 KB |
Output is correct |
70 |
Correct |
151 ms |
19868 KB |
Output is correct |
71 |
Correct |
139 ms |
19888 KB |
Output is correct |
72 |
Correct |
121 ms |
19852 KB |
Output is correct |
73 |
Correct |
237 ms |
40040 KB |
Output is correct |
74 |
Correct |
243 ms |
40052 KB |
Output is correct |
75 |
Correct |
188 ms |
19804 KB |
Output is correct |
76 |
Correct |
207 ms |
19760 KB |
Output is correct |
77 |
Correct |
196 ms |
19772 KB |
Output is correct |
78 |
Correct |
199 ms |
19896 KB |
Output is correct |
79 |
Correct |
205 ms |
19800 KB |
Output is correct |
80 |
Correct |
173 ms |
19720 KB |
Output is correct |
81 |
Correct |
165 ms |
20212 KB |
Output is correct |
82 |
Correct |
156 ms |
20000 KB |
Output is correct |
83 |
Correct |
233 ms |
28088 KB |
Output is correct |
84 |
Correct |
237 ms |
31504 KB |
Output is correct |
85 |
Correct |
222 ms |
30924 KB |
Output is correct |
86 |
Correct |
199 ms |
21960 KB |
Output is correct |
87 |
Correct |
175 ms |
21044 KB |
Output is correct |
88 |
Correct |
204 ms |
21448 KB |
Output is correct |
89 |
Correct |
223 ms |
21468 KB |
Output is correct |
90 |
Correct |
232 ms |
21120 KB |
Output is correct |
91 |
Correct |
231 ms |
21504 KB |
Output is correct |
92 |
Correct |
190 ms |
22056 KB |
Output is correct |
93 |
Correct |
206 ms |
26032 KB |
Output is correct |
94 |
Correct |
218 ms |
28848 KB |
Output is correct |
95 |
Correct |
196 ms |
26732 KB |
Output is correct |
96 |
Correct |
207 ms |
23808 KB |
Output is correct |
97 |
Correct |
236 ms |
21280 KB |
Output is correct |
98 |
Correct |
223 ms |
20492 KB |
Output is correct |
99 |
Correct |
248 ms |
20300 KB |
Output is correct |
100 |
Correct |
238 ms |
25260 KB |
Output is correct |
101 |
Correct |
207 ms |
22136 KB |
Output is correct |
102 |
Correct |
204 ms |
20828 KB |
Output is correct |
103 |
Correct |
219 ms |
20460 KB |
Output is correct |
104 |
Correct |
75 ms |
20692 KB |
Output is correct |
105 |
Correct |
209 ms |
34944 KB |
Output is correct |
106 |
Correct |
117 ms |
20616 KB |
Output is correct |
107 |
Correct |
129 ms |
20540 KB |
Output is correct |
108 |
Correct |
295 ms |
41464 KB |
Output is correct |
109 |
Correct |
102 ms |
20704 KB |
Output is correct |
110 |
Correct |
282 ms |
41800 KB |
Output is correct |
111 |
Correct |
208 ms |
19748 KB |
Output is correct |