#include<bits/stdc++.h>
using namespace std;
vector<int> ad[200001];
int dp[200001][2];
bool visited[200001], light[200001];
void dfs(int u)
{
visited[u] = 1;
for(int v : ad[u]) if(visited[v] == 0) dfs(v);
int sum = 0;
for(int v : ad[u]) if(visited[v] == 0) sum += dp[v][1];
dp[u][1] = max((int)light[u], sum - light[u]);
//calculate dp[u][0]
//Not activated case
int maxdif = 0, case1 = 0;
for(int v : ad[u]) if(visited[v] == 0){
maxdif = max(maxdif, max(dp[v][0], dp[v][1]));
case1 += dp[v][1];
}
if(ad[u].size()+(u==1) > 2) case1 -= light[u];
case1 = max(case1, maxdif);
//Activated case
int case2 = 0, sumcase = 0;
for(int v : ad[u]) if(visited[v] == 0){
case2 = max(case2, dp[v][1]);
sumcase += dp[v][1];
}
if(ad[u].size()+(u==1) > 2) sumcase -= light[u];
case2 += light[u];
case2 = max(case2, sumcase);
dp[u][0] = max(case1, case2);
visited[u] = 0;
//cout<<u<<" "<<dp[u][0]<<" "<<dp[u][1]<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for(int i = 0; i < n-1; i++){
int u, v;
cin>>u>>v;
ad[u].push_back(v);
ad[v].push_back(u);
}
for(int i = 1; i <= n; i++){
char x;
cin>>x;
light[i] = (x == '1');
}
dfs(1);
cout<<max(dp[1][0], dp[1][1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4976 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4976 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 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 |
4992 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 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 |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
3 ms |
5124 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
2 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5076 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4976 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 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 |
4992 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 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 |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
3 ms |
5124 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
2 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5076 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
92 ms |
13336 KB |
Output is correct |
65 |
Correct |
101 ms |
13300 KB |
Output is correct |
66 |
Correct |
69 ms |
13260 KB |
Output is correct |
67 |
Correct |
76 ms |
13296 KB |
Output is correct |
68 |
Correct |
83 ms |
13260 KB |
Output is correct |
69 |
Correct |
101 ms |
13516 KB |
Output is correct |
70 |
Correct |
80 ms |
13224 KB |
Output is correct |
71 |
Correct |
73 ms |
13260 KB |
Output is correct |
72 |
Correct |
88 ms |
13332 KB |
Output is correct |
73 |
Correct |
95 ms |
25848 KB |
Output is correct |
74 |
Correct |
128 ms |
25824 KB |
Output is correct |
75 |
Correct |
67 ms |
13132 KB |
Output is correct |
76 |
Correct |
68 ms |
13132 KB |
Output is correct |
77 |
Correct |
69 ms |
13212 KB |
Output is correct |
78 |
Correct |
76 ms |
13104 KB |
Output is correct |
79 |
Correct |
95 ms |
13120 KB |
Output is correct |
80 |
Correct |
96 ms |
13152 KB |
Output is correct |
81 |
Correct |
75 ms |
13572 KB |
Output is correct |
82 |
Correct |
73 ms |
13448 KB |
Output is correct |
83 |
Correct |
85 ms |
18420 KB |
Output is correct |
84 |
Correct |
92 ms |
20540 KB |
Output is correct |
85 |
Correct |
93 ms |
20172 KB |
Output is correct |
86 |
Correct |
80 ms |
14568 KB |
Output is correct |
87 |
Correct |
78 ms |
14100 KB |
Output is correct |
88 |
Correct |
68 ms |
14468 KB |
Output is correct |
89 |
Correct |
78 ms |
14572 KB |
Output is correct |
90 |
Correct |
89 ms |
14284 KB |
Output is correct |
91 |
Correct |
72 ms |
14492 KB |
Output is correct |
92 |
Correct |
74 ms |
14852 KB |
Output is correct |
93 |
Correct |
77 ms |
17280 KB |
Output is correct |
94 |
Correct |
74 ms |
19024 KB |
Output is correct |
95 |
Correct |
84 ms |
17608 KB |
Output is correct |
96 |
Correct |
76 ms |
15820 KB |
Output is correct |
97 |
Correct |
96 ms |
14388 KB |
Output is correct |
98 |
Correct |
69 ms |
13900 KB |
Output is correct |
99 |
Correct |
72 ms |
13704 KB |
Output is correct |
100 |
Correct |
75 ms |
16844 KB |
Output is correct |
101 |
Correct |
73 ms |
14816 KB |
Output is correct |
102 |
Correct |
126 ms |
14172 KB |
Output is correct |
103 |
Correct |
76 ms |
13900 KB |
Output is correct |
104 |
Correct |
58 ms |
13972 KB |
Output is correct |
105 |
Correct |
149 ms |
22680 KB |
Output is correct |
106 |
Correct |
50 ms |
13892 KB |
Output is correct |
107 |
Correct |
64 ms |
13976 KB |
Output is correct |
108 |
Correct |
89 ms |
26600 KB |
Output is correct |
109 |
Correct |
59 ms |
13924 KB |
Output is correct |
110 |
Correct |
109 ms |
27032 KB |
Output is correct |
111 |
Correct |
123 ms |
13260 KB |
Output is correct |