#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in array<int, 2>
#define pb push_back
#define pob pop_back
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
const int MX = 2e5+5;
const int INF = 1e17;
vector<int> adj[MX];
bool S[MX];
int dp[2][2][MX];
void dfs(int u, int p)
{
int V = 0; int W = 0; int UU = 0;
int rr = 0;
for(int v: adj[u])
{
if(v==p)
continue;
rr++;
dfs(v, u);
V = max(V, max(dp[0][0][v], dp[0][1][v]));
UU = max(UU, max(dp[1][0][v], dp[1][1][v]));
W+=max(dp[1][0][v], dp[1][1][v]);
}
if(rr == 0)
{
dp[0][0][u] = 0;
dp[0][1][u] = S[u];
dp[1][0][u] = 0;
dp[1][1][u] = S[u];
return;
}
//Case: Nothing selected at u, or its parent.
//We ignore the root, and compute cost without it. Yet, we enable the foundation.
dp[0][0][u] = max(0ll, max(V, W-S[u]));
dp[0][1][u] = max(0ll, max(UU+S[u], W-S[u]));
dp[1][0][u] = max(0ll, max(UU-S[u], W-S[u]));
dp[1][1][u] = max(0ll, max(S[u]+0ll, max(UU-S[u], W-S[u])));
return;
}
signed main()
{
fast();
int n;
cin >> n;
for(int i = 1; i < n; i++)
{
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
for(int i = 1; i <= n; i++)
{
char c; cin >> c;
S[i] = (c=='1');
}
dfs(1, 0);
cout << (max(dp[0][1][1], dp[0][0][1])) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11352 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11192 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
3 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
3 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11352 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11192 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
3 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
3 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
3 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11360 KB |
Output is correct |
25 |
Correct |
3 ms |
11360 KB |
Output is correct |
26 |
Correct |
3 ms |
11360 KB |
Output is correct |
27 |
Correct |
3 ms |
11448 KB |
Output is correct |
28 |
Correct |
3 ms |
11352 KB |
Output is correct |
29 |
Correct |
4 ms |
11616 KB |
Output is correct |
30 |
Correct |
2 ms |
11612 KB |
Output is correct |
31 |
Correct |
3 ms |
11356 KB |
Output is correct |
32 |
Correct |
3 ms |
11356 KB |
Output is correct |
33 |
Correct |
3 ms |
11356 KB |
Output is correct |
34 |
Correct |
3 ms |
11436 KB |
Output is correct |
35 |
Correct |
3 ms |
11356 KB |
Output is correct |
36 |
Correct |
3 ms |
11356 KB |
Output is correct |
37 |
Correct |
3 ms |
11432 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11356 KB |
Output is correct |
40 |
Correct |
3 ms |
11356 KB |
Output is correct |
41 |
Correct |
3 ms |
11612 KB |
Output is correct |
42 |
Correct |
3 ms |
11356 KB |
Output is correct |
43 |
Correct |
3 ms |
11356 KB |
Output is correct |
44 |
Correct |
2 ms |
11488 KB |
Output is correct |
45 |
Correct |
3 ms |
11356 KB |
Output is correct |
46 |
Correct |
3 ms |
11352 KB |
Output is correct |
47 |
Correct |
3 ms |
11356 KB |
Output is correct |
48 |
Correct |
3 ms |
11612 KB |
Output is correct |
49 |
Correct |
3 ms |
11612 KB |
Output is correct |
50 |
Correct |
3 ms |
11356 KB |
Output is correct |
51 |
Correct |
3 ms |
11356 KB |
Output is correct |
52 |
Correct |
3 ms |
11356 KB |
Output is correct |
53 |
Correct |
3 ms |
11356 KB |
Output is correct |
54 |
Correct |
3 ms |
11356 KB |
Output is correct |
55 |
Correct |
3 ms |
11352 KB |
Output is correct |
56 |
Correct |
3 ms |
11352 KB |
Output is correct |
57 |
Correct |
3 ms |
11612 KB |
Output is correct |
58 |
Correct |
3 ms |
11536 KB |
Output is correct |
59 |
Correct |
3 ms |
11356 KB |
Output is correct |
60 |
Correct |
3 ms |
11612 KB |
Output is correct |
61 |
Correct |
3 ms |
11356 KB |
Output is correct |
62 |
Correct |
3 ms |
11472 KB |
Output is correct |
63 |
Correct |
3 ms |
11440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11352 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11192 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
3 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
3 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
3 ms |
11356 KB |
Output is correct |
24 |
Correct |
3 ms |
11360 KB |
Output is correct |
25 |
Correct |
3 ms |
11360 KB |
Output is correct |
26 |
Correct |
3 ms |
11360 KB |
Output is correct |
27 |
Correct |
3 ms |
11448 KB |
Output is correct |
28 |
Correct |
3 ms |
11352 KB |
Output is correct |
29 |
Correct |
4 ms |
11616 KB |
Output is correct |
30 |
Correct |
2 ms |
11612 KB |
Output is correct |
31 |
Correct |
3 ms |
11356 KB |
Output is correct |
32 |
Correct |
3 ms |
11356 KB |
Output is correct |
33 |
Correct |
3 ms |
11356 KB |
Output is correct |
34 |
Correct |
3 ms |
11436 KB |
Output is correct |
35 |
Correct |
3 ms |
11356 KB |
Output is correct |
36 |
Correct |
3 ms |
11356 KB |
Output is correct |
37 |
Correct |
3 ms |
11432 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11356 KB |
Output is correct |
40 |
Correct |
3 ms |
11356 KB |
Output is correct |
41 |
Correct |
3 ms |
11612 KB |
Output is correct |
42 |
Correct |
3 ms |
11356 KB |
Output is correct |
43 |
Correct |
3 ms |
11356 KB |
Output is correct |
44 |
Correct |
2 ms |
11488 KB |
Output is correct |
45 |
Correct |
3 ms |
11356 KB |
Output is correct |
46 |
Correct |
3 ms |
11352 KB |
Output is correct |
47 |
Correct |
3 ms |
11356 KB |
Output is correct |
48 |
Correct |
3 ms |
11612 KB |
Output is correct |
49 |
Correct |
3 ms |
11612 KB |
Output is correct |
50 |
Correct |
3 ms |
11356 KB |
Output is correct |
51 |
Correct |
3 ms |
11356 KB |
Output is correct |
52 |
Correct |
3 ms |
11356 KB |
Output is correct |
53 |
Correct |
3 ms |
11356 KB |
Output is correct |
54 |
Correct |
3 ms |
11356 KB |
Output is correct |
55 |
Correct |
3 ms |
11352 KB |
Output is correct |
56 |
Correct |
3 ms |
11352 KB |
Output is correct |
57 |
Correct |
3 ms |
11612 KB |
Output is correct |
58 |
Correct |
3 ms |
11536 KB |
Output is correct |
59 |
Correct |
3 ms |
11356 KB |
Output is correct |
60 |
Correct |
3 ms |
11612 KB |
Output is correct |
61 |
Correct |
3 ms |
11356 KB |
Output is correct |
62 |
Correct |
3 ms |
11472 KB |
Output is correct |
63 |
Correct |
3 ms |
11440 KB |
Output is correct |
64 |
Correct |
82 ms |
21584 KB |
Output is correct |
65 |
Correct |
88 ms |
21612 KB |
Output is correct |
66 |
Correct |
92 ms |
21676 KB |
Output is correct |
67 |
Correct |
96 ms |
21668 KB |
Output is correct |
68 |
Correct |
81 ms |
21592 KB |
Output is correct |
69 |
Correct |
84 ms |
21588 KB |
Output is correct |
70 |
Correct |
84 ms |
21464 KB |
Output is correct |
71 |
Correct |
88 ms |
21588 KB |
Output is correct |
72 |
Correct |
81 ms |
21920 KB |
Output is correct |
73 |
Correct |
98 ms |
35736 KB |
Output is correct |
74 |
Correct |
116 ms |
35660 KB |
Output is correct |
75 |
Correct |
102 ms |
22772 KB |
Output is correct |
76 |
Correct |
91 ms |
22868 KB |
Output is correct |
77 |
Correct |
84 ms |
21584 KB |
Output is correct |
78 |
Correct |
120 ms |
21400 KB |
Output is correct |
79 |
Correct |
89 ms |
21580 KB |
Output is correct |
80 |
Correct |
88 ms |
21588 KB |
Output is correct |
81 |
Correct |
67 ms |
21456 KB |
Output is correct |
82 |
Correct |
74 ms |
21520 KB |
Output is correct |
83 |
Correct |
102 ms |
27732 KB |
Output is correct |
84 |
Correct |
111 ms |
30156 KB |
Output is correct |
85 |
Correct |
99 ms |
29820 KB |
Output is correct |
86 |
Correct |
88 ms |
23124 KB |
Output is correct |
87 |
Correct |
88 ms |
22460 KB |
Output is correct |
88 |
Correct |
95 ms |
22736 KB |
Output is correct |
89 |
Correct |
82 ms |
22804 KB |
Output is correct |
90 |
Correct |
84 ms |
22444 KB |
Output is correct |
91 |
Correct |
78 ms |
22628 KB |
Output is correct |
92 |
Correct |
88 ms |
23116 KB |
Output is correct |
93 |
Correct |
81 ms |
25544 KB |
Output is correct |
94 |
Correct |
84 ms |
27584 KB |
Output is correct |
95 |
Correct |
81 ms |
26048 KB |
Output is correct |
96 |
Correct |
87 ms |
24148 KB |
Output is correct |
97 |
Correct |
79 ms |
22392 KB |
Output is correct |
98 |
Correct |
79 ms |
21844 KB |
Output is correct |
99 |
Correct |
87 ms |
22000 KB |
Output is correct |
100 |
Correct |
81 ms |
25176 KB |
Output is correct |
101 |
Correct |
79 ms |
22612 KB |
Output is correct |
102 |
Correct |
106 ms |
22088 KB |
Output is correct |
103 |
Correct |
84 ms |
21844 KB |
Output is correct |
104 |
Correct |
55 ms |
22212 KB |
Output is correct |
105 |
Correct |
92 ms |
31828 KB |
Output is correct |
106 |
Correct |
52 ms |
21956 KB |
Output is correct |
107 |
Correct |
63 ms |
22272 KB |
Output is correct |
108 |
Correct |
98 ms |
36688 KB |
Output is correct |
109 |
Correct |
52 ms |
22460 KB |
Output is correct |
110 |
Correct |
109 ms |
36936 KB |
Output is correct |
111 |
Correct |
72 ms |
21584 KB |
Output is correct |