#include "bits/stdc++.h"
using namespace std;
#define ar array
//~ #define int long long
const int N = 2e5 + 5;
vector<int> edges[N];
int a[N], dp[N], res, up[N];
void dfs(int u, int p = -1){
for(auto x : edges[u]){
if(x == p) continue;
dfs(x, u);
dp[u] += dp[x];
}
dp[u] = max(dp[u] - a[u], a[u]);
}
void redfs(int u, int p = -1){
for(auto x : edges[u]){
if(x == p) continue;
up[x] += up[u];
up[u] += dp[x];
} up[u] = max(up[u] - a[u], a[u]);
res = max(up[u], res);
up[u] = 0;
reverse(edges[u].begin(), edges[u].end());
for(auto x : edges[u]){
if(x == p) continue;
up[x] += up[u];
up[u] += dp[x];
up[x] = max(up[x] - a[u], a[u]);
redfs(x, u);
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
} for(int i=1;i<=n;i++){
char c; cin>>c;
a[i] = c - '0';
}
for(int i=1;i<=n;i++){
for(auto x : edges[i]){
if(a[x] && a[i]) res = 2;
}
}
dfs(1);
redfs(1);
cout<<res<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
6 ms |
5068 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5120 KB |
Output is correct |
49 |
Correct |
4 ms |
5068 KB |
Output is correct |
50 |
Correct |
5 ms |
5068 KB |
Output is correct |
51 |
Correct |
3 ms |
5024 KB |
Output is correct |
52 |
Correct |
3 ms |
5068 KB |
Output is correct |
53 |
Correct |
4 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5068 KB |
Output is correct |
57 |
Correct |
3 ms |
5196 KB |
Output is correct |
58 |
Correct |
4 ms |
5068 KB |
Output is correct |
59 |
Correct |
4 ms |
5116 KB |
Output is correct |
60 |
Correct |
3 ms |
5068 KB |
Output is correct |
61 |
Correct |
3 ms |
5068 KB |
Output is correct |
62 |
Correct |
3 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
6 ms |
5068 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5120 KB |
Output is correct |
49 |
Correct |
4 ms |
5068 KB |
Output is correct |
50 |
Correct |
5 ms |
5068 KB |
Output is correct |
51 |
Correct |
3 ms |
5024 KB |
Output is correct |
52 |
Correct |
3 ms |
5068 KB |
Output is correct |
53 |
Correct |
4 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5068 KB |
Output is correct |
57 |
Correct |
3 ms |
5196 KB |
Output is correct |
58 |
Correct |
4 ms |
5068 KB |
Output is correct |
59 |
Correct |
4 ms |
5116 KB |
Output is correct |
60 |
Correct |
3 ms |
5068 KB |
Output is correct |
61 |
Correct |
3 ms |
5068 KB |
Output is correct |
62 |
Correct |
3 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
64 |
Correct |
170 ms |
16380 KB |
Output is correct |
65 |
Correct |
168 ms |
16424 KB |
Output is correct |
66 |
Correct |
207 ms |
16356 KB |
Output is correct |
67 |
Correct |
163 ms |
16548 KB |
Output is correct |
68 |
Correct |
160 ms |
16340 KB |
Output is correct |
69 |
Correct |
162 ms |
16348 KB |
Output is correct |
70 |
Correct |
138 ms |
16324 KB |
Output is correct |
71 |
Correct |
184 ms |
16424 KB |
Output is correct |
72 |
Correct |
209 ms |
16356 KB |
Output is correct |
73 |
Correct |
214 ms |
29020 KB |
Output is correct |
74 |
Correct |
192 ms |
28952 KB |
Output is correct |
75 |
Correct |
148 ms |
16320 KB |
Output is correct |
76 |
Correct |
151 ms |
16232 KB |
Output is correct |
77 |
Correct |
164 ms |
16320 KB |
Output is correct |
78 |
Correct |
161 ms |
16364 KB |
Output is correct |
79 |
Correct |
151 ms |
16280 KB |
Output is correct |
80 |
Correct |
146 ms |
16316 KB |
Output is correct |
81 |
Correct |
135 ms |
16580 KB |
Output is correct |
82 |
Correct |
133 ms |
16440 KB |
Output is correct |
83 |
Correct |
170 ms |
21496 KB |
Output is correct |
84 |
Correct |
171 ms |
23664 KB |
Output is correct |
85 |
Correct |
167 ms |
23240 KB |
Output is correct |
86 |
Correct |
147 ms |
17696 KB |
Output is correct |
87 |
Correct |
147 ms |
17220 KB |
Output is correct |
88 |
Correct |
133 ms |
17568 KB |
Output is correct |
89 |
Correct |
132 ms |
17564 KB |
Output is correct |
90 |
Correct |
138 ms |
17476 KB |
Output is correct |
91 |
Correct |
153 ms |
17632 KB |
Output is correct |
92 |
Correct |
154 ms |
17908 KB |
Output is correct |
93 |
Correct |
132 ms |
20388 KB |
Output is correct |
94 |
Correct |
151 ms |
21944 KB |
Output is correct |
95 |
Correct |
130 ms |
20804 KB |
Output is correct |
96 |
Correct |
129 ms |
18920 KB |
Output is correct |
97 |
Correct |
143 ms |
17796 KB |
Output is correct |
98 |
Correct |
126 ms |
16964 KB |
Output is correct |
99 |
Correct |
126 ms |
16708 KB |
Output is correct |
100 |
Correct |
138 ms |
20024 KB |
Output is correct |
101 |
Correct |
129 ms |
17932 KB |
Output is correct |
102 |
Correct |
130 ms |
17092 KB |
Output is correct |
103 |
Correct |
135 ms |
16940 KB |
Output is correct |
104 |
Correct |
87 ms |
17188 KB |
Output is correct |
105 |
Correct |
168 ms |
25796 KB |
Output is correct |
106 |
Correct |
76 ms |
16984 KB |
Output is correct |
107 |
Correct |
77 ms |
17044 KB |
Output is correct |
108 |
Correct |
155 ms |
29796 KB |
Output is correct |
109 |
Correct |
84 ms |
17132 KB |
Output is correct |
110 |
Correct |
165 ms |
30124 KB |
Output is correct |
111 |
Correct |
112 ms |
16292 KB |
Output is correct |