#include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=5e5;int n,dp[N][2],is[N];vector<int> G[N];void dfs(int v,int p=-1) {int sum0=0,sum1=0,mx1=0,mx0=0;for(int u:G[v]) if(u!=p) {dfs(u,v);sum0+=dp[u][0];sum1+=dp[u][1];mx1=max(mx1,dp[u][1]);mx0=max(mx0,dp[u][0]);}dp[v][0]=(is[v]?max({mx0,sum1-1,1+mx1}):max({mx0,sum1}));dp[v][1]=(is[v]?max({1,sum1-1}):sum1);}int main(){cin>>n;for(int i=1; i<n ; i++) {int v,u;cin>>v>>u;G[v].pb(u);G[u].pb(v);}for(int i=1;i<=n;i++) {char c; cin>>c;is[i]=(c=='1');}dfs(1);cout<<dp[1][0]<<'\n';}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
5 ms |
14936 KB |
Output is correct |
4 |
Correct |
5 ms |
14936 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
4 ms |
14936 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
5 ms |
14968 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
5 ms |
14936 KB |
Output is correct |
15 |
Correct |
6 ms |
14936 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14936 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14936 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
5 ms |
14936 KB |
Output is correct |
4 |
Correct |
5 ms |
14936 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
4 ms |
14936 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
5 ms |
14968 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
5 ms |
14936 KB |
Output is correct |
15 |
Correct |
6 ms |
14936 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14936 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14936 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14956 KB |
Output is correct |
22 |
Correct |
5 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
5 ms |
14940 KB |
Output is correct |
25 |
Correct |
6 ms |
14940 KB |
Output is correct |
26 |
Correct |
7 ms |
14936 KB |
Output is correct |
27 |
Correct |
5 ms |
14940 KB |
Output is correct |
28 |
Correct |
5 ms |
14940 KB |
Output is correct |
29 |
Correct |
6 ms |
15196 KB |
Output is correct |
30 |
Correct |
5 ms |
14936 KB |
Output is correct |
31 |
Correct |
5 ms |
14936 KB |
Output is correct |
32 |
Correct |
6 ms |
14940 KB |
Output is correct |
33 |
Correct |
5 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
5 ms |
14940 KB |
Output is correct |
36 |
Correct |
5 ms |
15052 KB |
Output is correct |
37 |
Correct |
6 ms |
14940 KB |
Output is correct |
38 |
Correct |
6 ms |
14940 KB |
Output is correct |
39 |
Correct |
5 ms |
14940 KB |
Output is correct |
40 |
Correct |
5 ms |
14940 KB |
Output is correct |
41 |
Correct |
5 ms |
15044 KB |
Output is correct |
42 |
Correct |
7 ms |
15196 KB |
Output is correct |
43 |
Correct |
5 ms |
14940 KB |
Output is correct |
44 |
Correct |
5 ms |
15060 KB |
Output is correct |
45 |
Correct |
5 ms |
15192 KB |
Output is correct |
46 |
Correct |
5 ms |
14968 KB |
Output is correct |
47 |
Correct |
6 ms |
14936 KB |
Output is correct |
48 |
Correct |
6 ms |
14940 KB |
Output is correct |
49 |
Correct |
5 ms |
14940 KB |
Output is correct |
50 |
Correct |
5 ms |
14960 KB |
Output is correct |
51 |
Correct |
5 ms |
14940 KB |
Output is correct |
52 |
Correct |
5 ms |
14968 KB |
Output is correct |
53 |
Correct |
6 ms |
14940 KB |
Output is correct |
54 |
Correct |
5 ms |
14940 KB |
Output is correct |
55 |
Correct |
5 ms |
14940 KB |
Output is correct |
56 |
Correct |
5 ms |
14936 KB |
Output is correct |
57 |
Correct |
6 ms |
15196 KB |
Output is correct |
58 |
Correct |
5 ms |
14940 KB |
Output is correct |
59 |
Correct |
5 ms |
15004 KB |
Output is correct |
60 |
Correct |
6 ms |
14940 KB |
Output is correct |
61 |
Correct |
5 ms |
14996 KB |
Output is correct |
62 |
Correct |
6 ms |
14992 KB |
Output is correct |
63 |
Correct |
5 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
5 ms |
14936 KB |
Output is correct |
4 |
Correct |
5 ms |
14936 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
4 ms |
14936 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
5 ms |
14968 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
5 ms |
14936 KB |
Output is correct |
15 |
Correct |
6 ms |
14936 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14936 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14936 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14956 KB |
Output is correct |
22 |
Correct |
5 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
5 ms |
14940 KB |
Output is correct |
25 |
Correct |
6 ms |
14940 KB |
Output is correct |
26 |
Correct |
7 ms |
14936 KB |
Output is correct |
27 |
Correct |
5 ms |
14940 KB |
Output is correct |
28 |
Correct |
5 ms |
14940 KB |
Output is correct |
29 |
Correct |
6 ms |
15196 KB |
Output is correct |
30 |
Correct |
5 ms |
14936 KB |
Output is correct |
31 |
Correct |
5 ms |
14936 KB |
Output is correct |
32 |
Correct |
6 ms |
14940 KB |
Output is correct |
33 |
Correct |
5 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
5 ms |
14940 KB |
Output is correct |
36 |
Correct |
5 ms |
15052 KB |
Output is correct |
37 |
Correct |
6 ms |
14940 KB |
Output is correct |
38 |
Correct |
6 ms |
14940 KB |
Output is correct |
39 |
Correct |
5 ms |
14940 KB |
Output is correct |
40 |
Correct |
5 ms |
14940 KB |
Output is correct |
41 |
Correct |
5 ms |
15044 KB |
Output is correct |
42 |
Correct |
7 ms |
15196 KB |
Output is correct |
43 |
Correct |
5 ms |
14940 KB |
Output is correct |
44 |
Correct |
5 ms |
15060 KB |
Output is correct |
45 |
Correct |
5 ms |
15192 KB |
Output is correct |
46 |
Correct |
5 ms |
14968 KB |
Output is correct |
47 |
Correct |
6 ms |
14936 KB |
Output is correct |
48 |
Correct |
6 ms |
14940 KB |
Output is correct |
49 |
Correct |
5 ms |
14940 KB |
Output is correct |
50 |
Correct |
5 ms |
14960 KB |
Output is correct |
51 |
Correct |
5 ms |
14940 KB |
Output is correct |
52 |
Correct |
5 ms |
14968 KB |
Output is correct |
53 |
Correct |
6 ms |
14940 KB |
Output is correct |
54 |
Correct |
5 ms |
14940 KB |
Output is correct |
55 |
Correct |
5 ms |
14940 KB |
Output is correct |
56 |
Correct |
5 ms |
14936 KB |
Output is correct |
57 |
Correct |
6 ms |
15196 KB |
Output is correct |
58 |
Correct |
5 ms |
14940 KB |
Output is correct |
59 |
Correct |
5 ms |
15004 KB |
Output is correct |
60 |
Correct |
6 ms |
14940 KB |
Output is correct |
61 |
Correct |
5 ms |
14996 KB |
Output is correct |
62 |
Correct |
6 ms |
14992 KB |
Output is correct |
63 |
Correct |
5 ms |
14940 KB |
Output is correct |
64 |
Correct |
169 ms |
23228 KB |
Output is correct |
65 |
Correct |
161 ms |
23224 KB |
Output is correct |
66 |
Correct |
174 ms |
23384 KB |
Output is correct |
67 |
Correct |
171 ms |
23364 KB |
Output is correct |
68 |
Correct |
175 ms |
23392 KB |
Output is correct |
69 |
Correct |
149 ms |
23376 KB |
Output is correct |
70 |
Correct |
172 ms |
23376 KB |
Output is correct |
71 |
Correct |
166 ms |
23368 KB |
Output is correct |
72 |
Correct |
146 ms |
23240 KB |
Output is correct |
73 |
Correct |
187 ms |
38488 KB |
Output is correct |
74 |
Correct |
202 ms |
38460 KB |
Output is correct |
75 |
Correct |
163 ms |
23124 KB |
Output is correct |
76 |
Correct |
155 ms |
23116 KB |
Output is correct |
77 |
Correct |
199 ms |
23376 KB |
Output is correct |
78 |
Correct |
180 ms |
23124 KB |
Output is correct |
79 |
Correct |
185 ms |
23124 KB |
Output is correct |
80 |
Correct |
149 ms |
23276 KB |
Output is correct |
81 |
Correct |
157 ms |
23496 KB |
Output is correct |
82 |
Correct |
151 ms |
23388 KB |
Output is correct |
83 |
Correct |
188 ms |
29392 KB |
Output is correct |
84 |
Correct |
198 ms |
32160 KB |
Output is correct |
85 |
Correct |
165 ms |
31568 KB |
Output is correct |
86 |
Correct |
164 ms |
24828 KB |
Output is correct |
87 |
Correct |
204 ms |
24160 KB |
Output is correct |
88 |
Correct |
151 ms |
24656 KB |
Output is correct |
89 |
Correct |
193 ms |
24768 KB |
Output is correct |
90 |
Correct |
176 ms |
24376 KB |
Output is correct |
91 |
Correct |
153 ms |
24660 KB |
Output is correct |
92 |
Correct |
154 ms |
25076 KB |
Output is correct |
93 |
Correct |
168 ms |
28108 KB |
Output is correct |
94 |
Correct |
204 ms |
30048 KB |
Output is correct |
95 |
Correct |
202 ms |
28364 KB |
Output is correct |
96 |
Correct |
158 ms |
26340 KB |
Output is correct |
97 |
Correct |
171 ms |
24584 KB |
Output is correct |
98 |
Correct |
155 ms |
23776 KB |
Output is correct |
99 |
Correct |
145 ms |
23512 KB |
Output is correct |
100 |
Correct |
184 ms |
27628 KB |
Output is correct |
101 |
Correct |
167 ms |
25140 KB |
Output is correct |
102 |
Correct |
190 ms |
24032 KB |
Output is correct |
103 |
Correct |
186 ms |
24232 KB |
Output is correct |
104 |
Correct |
133 ms |
23888 KB |
Output is correct |
105 |
Correct |
199 ms |
34644 KB |
Output is correct |
106 |
Correct |
128 ms |
23864 KB |
Output is correct |
107 |
Correct |
144 ms |
24096 KB |
Output is correct |
108 |
Correct |
186 ms |
39408 KB |
Output is correct |
109 |
Correct |
132 ms |
24008 KB |
Output is correct |
110 |
Correct |
193 ms |
39796 KB |
Output is correct |
111 |
Correct |
159 ms |
23124 KB |
Output is correct |