#include "bits/stdc++.h"
using namespace std;
const int N=200001;
int n;
vector<int> adj[N];
int dp[N][2];
int up[N];
string s;
int ans=1;
void dfs(int x, int par){
dp[x][1]=(s[x]=='1'?1:-1e9);
dp[x][0]=(s[x]=='1')*(-1);
for(int k:adj[x]){
if(k==par) continue;
dfs(k, x);
dp[x][0]+=max({0, dp[k][0], dp[k][1]});
if(s[x]=='1' && s[k]=='1') ans=max(ans, 2);
}
// cout << "dp[" << x << "][" << 0 << "] " << dp[x][0] << '\n';
}
void dfs2(int x, int par){
up[x]=max(up[x], 0);
// cout << "up " << x << " " << up[x] << "\n";
int sum=up[x];
for(int k:adj[x]){
if(k==par) continue;
sum+=max({0, dp[k][0], dp[k][1]});
}
for(int k:adj[x]){
if(k==par) continue;
up[k]=max(dp[x][1], (s[x]=='1')*(-1)+sum-max({0, dp[k][0], dp[k][1]}));
dfs2(k, x);
ans=max(ans, dp[k][0]+up[k]);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
s='0'+s;
dfs(1, 0);
dfs2(1, 0);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
5008 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
7 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
4 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
5008 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
7 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
4 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
5 ms |
5072 KB |
Output is correct |
23 |
Correct |
5 ms |
5004 KB |
Output is correct |
24 |
Correct |
6 ms |
5068 KB |
Output is correct |
25 |
Correct |
6 ms |
5084 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5152 KB |
Output is correct |
30 |
Correct |
7 ms |
5148 KB |
Output is correct |
31 |
Correct |
7 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5028 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
5 ms |
5068 KB |
Output is correct |
37 |
Correct |
5 ms |
5080 KB |
Output is correct |
38 |
Correct |
3 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5196 KB |
Output is correct |
40 |
Correct |
5 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5196 KB |
Output is correct |
42 |
Correct |
5 ms |
5016 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5152 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5156 KB |
Output is correct |
49 |
Correct |
3 ms |
5152 KB |
Output is correct |
50 |
Correct |
3 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5160 KB |
Output is correct |
52 |
Correct |
4 ms |
5080 KB |
Output is correct |
53 |
Correct |
4 ms |
5072 KB |
Output is correct |
54 |
Correct |
4 ms |
5072 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5068 KB |
Output is correct |
57 |
Correct |
4 ms |
5196 KB |
Output is correct |
58 |
Correct |
3 ms |
5068 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
5008 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
7 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5012 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
4 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
5 ms |
5072 KB |
Output is correct |
23 |
Correct |
5 ms |
5004 KB |
Output is correct |
24 |
Correct |
6 ms |
5068 KB |
Output is correct |
25 |
Correct |
6 ms |
5084 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5152 KB |
Output is correct |
30 |
Correct |
7 ms |
5148 KB |
Output is correct |
31 |
Correct |
7 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5028 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
5 ms |
5068 KB |
Output is correct |
37 |
Correct |
5 ms |
5080 KB |
Output is correct |
38 |
Correct |
3 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5196 KB |
Output is correct |
40 |
Correct |
5 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5196 KB |
Output is correct |
42 |
Correct |
5 ms |
5016 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5152 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5156 KB |
Output is correct |
49 |
Correct |
3 ms |
5152 KB |
Output is correct |
50 |
Correct |
3 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5160 KB |
Output is correct |
52 |
Correct |
4 ms |
5080 KB |
Output is correct |
53 |
Correct |
4 ms |
5072 KB |
Output is correct |
54 |
Correct |
4 ms |
5072 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5068 KB |
Output is correct |
57 |
Correct |
4 ms |
5196 KB |
Output is correct |
58 |
Correct |
3 ms |
5068 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
3 ms |
5068 KB |
Output is correct |
64 |
Correct |
189 ms |
16756 KB |
Output is correct |
65 |
Correct |
215 ms |
16716 KB |
Output is correct |
66 |
Correct |
188 ms |
16700 KB |
Output is correct |
67 |
Correct |
231 ms |
16680 KB |
Output is correct |
68 |
Correct |
166 ms |
16704 KB |
Output is correct |
69 |
Correct |
195 ms |
16716 KB |
Output is correct |
70 |
Correct |
221 ms |
16756 KB |
Output is correct |
71 |
Correct |
214 ms |
16752 KB |
Output is correct |
72 |
Correct |
192 ms |
16752 KB |
Output is correct |
73 |
Correct |
231 ms |
29288 KB |
Output is correct |
74 |
Correct |
286 ms |
29204 KB |
Output is correct |
75 |
Correct |
196 ms |
16644 KB |
Output is correct |
76 |
Correct |
199 ms |
16576 KB |
Output is correct |
77 |
Correct |
255 ms |
16628 KB |
Output is correct |
78 |
Correct |
183 ms |
16648 KB |
Output is correct |
79 |
Correct |
216 ms |
16620 KB |
Output is correct |
80 |
Correct |
181 ms |
16648 KB |
Output is correct |
81 |
Correct |
156 ms |
17052 KB |
Output is correct |
82 |
Correct |
182 ms |
16852 KB |
Output is correct |
83 |
Correct |
226 ms |
21756 KB |
Output is correct |
84 |
Correct |
188 ms |
23924 KB |
Output is correct |
85 |
Correct |
195 ms |
23652 KB |
Output is correct |
86 |
Correct |
251 ms |
18040 KB |
Output is correct |
87 |
Correct |
207 ms |
17460 KB |
Output is correct |
88 |
Correct |
221 ms |
17992 KB |
Output is correct |
89 |
Correct |
244 ms |
17912 KB |
Output is correct |
90 |
Correct |
197 ms |
17784 KB |
Output is correct |
91 |
Correct |
211 ms |
17984 KB |
Output is correct |
92 |
Correct |
150 ms |
18276 KB |
Output is correct |
93 |
Correct |
214 ms |
20700 KB |
Output is correct |
94 |
Correct |
151 ms |
22436 KB |
Output is correct |
95 |
Correct |
161 ms |
21044 KB |
Output is correct |
96 |
Correct |
217 ms |
19272 KB |
Output is correct |
97 |
Correct |
175 ms |
17740 KB |
Output is correct |
98 |
Correct |
174 ms |
17240 KB |
Output is correct |
99 |
Correct |
181 ms |
17180 KB |
Output is correct |
100 |
Correct |
172 ms |
20292 KB |
Output is correct |
101 |
Correct |
236 ms |
18220 KB |
Output is correct |
102 |
Correct |
154 ms |
17424 KB |
Output is correct |
103 |
Correct |
176 ms |
17328 KB |
Output is correct |
104 |
Correct |
153 ms |
17532 KB |
Output is correct |
105 |
Correct |
204 ms |
26120 KB |
Output is correct |
106 |
Correct |
90 ms |
17436 KB |
Output is correct |
107 |
Correct |
130 ms |
17432 KB |
Output is correct |
108 |
Correct |
213 ms |
30048 KB |
Output is correct |
109 |
Correct |
108 ms |
17632 KB |
Output is correct |
110 |
Correct |
218 ms |
30400 KB |
Output is correct |
111 |
Correct |
167 ms |
16576 KB |
Output is correct |