#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 2*1e5;
int n, dp[MAX_N][2];
string s;
vector<int> adj[MAX_N];
void find_dp(int id, int anc){
dp[id][0]= 0;
dp[id][1] = 0;
bool gen = s[id] == '1';
int s1=0;
int best1= 0;
int best0= 0;
for(auto e: adj[id]){
if(e!=anc){
find_dp(e, id);
s1 += dp[e][1];
best1 = max(best1, dp[e][1]);
best0 = max(best0, dp[e][0]);
}
}
if(!gen){
dp[id][0] = max(max(s1, best0), 0LL);
dp[id][1] = max(s1, 0LL);
}
else{
dp[id][0] = max(s1-1, max(best1+1, best0));
dp[id][1] = max(s1-1, 1LL);
}
//cout<<id<<" "<<dp[id][0]<<" "<<dp[id][1]<<endl;
}
signed main(){
cin>>n;
for(int i = 0; i<n-1; i++){
int a, b;
cin>>a>>b;
a--;b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>s;
find_dp(0, -1);
cout<<dp[0][0]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5008 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
5012 KB |
Output is correct |
4 |
Correct |
2 ms |
5004 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
5012 KB |
Output is correct |
12 |
Correct |
2 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 |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4920 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5008 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
5012 KB |
Output is correct |
4 |
Correct |
2 ms |
5004 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
5012 KB |
Output is correct |
12 |
Correct |
2 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 |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4920 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5004 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5084 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5028 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5148 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 |
5 ms |
5016 KB |
Output is correct |
38 |
Correct |
3 ms |
5016 KB |
Output is correct |
39 |
Correct |
3 ms |
5152 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5144 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5144 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5140 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5064 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 |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5008 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
5012 KB |
Output is correct |
4 |
Correct |
2 ms |
5004 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
5012 KB |
Output is correct |
12 |
Correct |
2 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 |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4920 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5004 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5084 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5028 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5148 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 |
5 ms |
5016 KB |
Output is correct |
38 |
Correct |
3 ms |
5016 KB |
Output is correct |
39 |
Correct |
3 ms |
5152 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5144 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5144 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5140 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5064 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 |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 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 |
138 ms |
18432 KB |
Output is correct |
65 |
Correct |
140 ms |
18468 KB |
Output is correct |
66 |
Correct |
144 ms |
18528 KB |
Output is correct |
67 |
Correct |
133 ms |
18524 KB |
Output is correct |
68 |
Correct |
150 ms |
18424 KB |
Output is correct |
69 |
Correct |
134 ms |
18504 KB |
Output is correct |
70 |
Correct |
135 ms |
18436 KB |
Output is correct |
71 |
Correct |
155 ms |
18464 KB |
Output is correct |
72 |
Correct |
134 ms |
18492 KB |
Output is correct |
73 |
Correct |
155 ms |
32632 KB |
Output is correct |
74 |
Correct |
196 ms |
32600 KB |
Output is correct |
75 |
Correct |
148 ms |
19812 KB |
Output is correct |
76 |
Correct |
139 ms |
19672 KB |
Output is correct |
77 |
Correct |
155 ms |
18496 KB |
Output is correct |
78 |
Correct |
142 ms |
18440 KB |
Output is correct |
79 |
Correct |
152 ms |
18572 KB |
Output is correct |
80 |
Correct |
156 ms |
18452 KB |
Output is correct |
81 |
Correct |
136 ms |
18532 KB |
Output is correct |
82 |
Correct |
132 ms |
18496 KB |
Output is correct |
83 |
Correct |
156 ms |
24648 KB |
Output is correct |
84 |
Correct |
150 ms |
27160 KB |
Output is correct |
85 |
Correct |
143 ms |
26784 KB |
Output is correct |
86 |
Correct |
175 ms |
20092 KB |
Output is correct |
87 |
Correct |
153 ms |
19420 KB |
Output is correct |
88 |
Correct |
135 ms |
19492 KB |
Output is correct |
89 |
Correct |
146 ms |
19484 KB |
Output is correct |
90 |
Correct |
133 ms |
19208 KB |
Output is correct |
91 |
Correct |
135 ms |
19496 KB |
Output is correct |
92 |
Correct |
157 ms |
19904 KB |
Output is correct |
93 |
Correct |
142 ms |
22744 KB |
Output is correct |
94 |
Correct |
134 ms |
24688 KB |
Output is correct |
95 |
Correct |
136 ms |
23232 KB |
Output is correct |
96 |
Correct |
132 ms |
20912 KB |
Output is correct |
97 |
Correct |
132 ms |
19236 KB |
Output is correct |
98 |
Correct |
127 ms |
18656 KB |
Output is correct |
99 |
Correct |
162 ms |
18524 KB |
Output is correct |
100 |
Correct |
181 ms |
22136 KB |
Output is correct |
101 |
Correct |
134 ms |
19648 KB |
Output is correct |
102 |
Correct |
132 ms |
18872 KB |
Output is correct |
103 |
Correct |
171 ms |
18820 KB |
Output is correct |
104 |
Correct |
130 ms |
19104 KB |
Output is correct |
105 |
Correct |
154 ms |
28872 KB |
Output is correct |
106 |
Correct |
113 ms |
19000 KB |
Output is correct |
107 |
Correct |
142 ms |
18928 KB |
Output is correct |
108 |
Correct |
151 ms |
33492 KB |
Output is correct |
109 |
Correct |
125 ms |
19164 KB |
Output is correct |
110 |
Correct |
160 ms |
33940 KB |
Output is correct |
111 |
Correct |
130 ms |
18468 KB |
Output is correct |