#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2e5 + 5;
template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
int n;
int dp[mxN][2];
string s;
vi g[mxN];
void dfs(int cur, int prev){
if(s[cur] == '1') dp[cur][0] = 1;
int tot = 0, mx = 0;
for(int x : g[cur]) if(x != prev){
dfs(x, cur);
tot += dp[x][0];
mx = max(mx, dp[x][0]);
}
dp[cur][0] = max(tot - (s[cur] == '1'), dp[cur][0]);
if(s[cur] == '1') dp[cur][1] = mx + 1;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cin >> n;
int cnt = 0;
rep(i, n - 1){
int a, b;
cin >> a >> b;
a--, b--;
g[a].pb(b);
g[b].pb(a);
}
cin >> s;
rep(i, n) cnt += (s[i] == '1');
int ans = 0, mn = 0;
if(cnt == 2) mn = 2;
dfs(0, -1);
rep(i, n) ans = max({ans, dp[i][0], dp[i][1]});
cout << max(mn, ans) << endl;
return 0;
}
/*
Things to look out for:
- Integer overflows
- Make sure that max is large enough (2e9, 4e18)
- Special cases
Be careful!
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
5 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5116 KB |
Output is correct |
19 |
Correct |
4 ms |
5228 KB |
Output is correct |
20 |
Correct |
4 ms |
5116 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
5 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5116 KB |
Output is correct |
19 |
Correct |
4 ms |
5228 KB |
Output is correct |
20 |
Correct |
4 ms |
5116 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5100 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5100 KB |
Output is correct |
25 |
Correct |
5 ms |
5100 KB |
Output is correct |
26 |
Correct |
5 ms |
5100 KB |
Output is correct |
27 |
Correct |
5 ms |
5100 KB |
Output is correct |
28 |
Correct |
7 ms |
5100 KB |
Output is correct |
29 |
Correct |
6 ms |
5228 KB |
Output is correct |
30 |
Correct |
5 ms |
5228 KB |
Output is correct |
31 |
Correct |
6 ms |
5100 KB |
Output is correct |
32 |
Correct |
5 ms |
5100 KB |
Output is correct |
33 |
Correct |
4 ms |
5100 KB |
Output is correct |
34 |
Correct |
4 ms |
5100 KB |
Output is correct |
35 |
Correct |
4 ms |
5100 KB |
Output is correct |
36 |
Correct |
5 ms |
5100 KB |
Output is correct |
37 |
Correct |
5 ms |
5100 KB |
Output is correct |
38 |
Correct |
5 ms |
5100 KB |
Output is correct |
39 |
Correct |
5 ms |
5228 KB |
Output is correct |
40 |
Correct |
5 ms |
5100 KB |
Output is correct |
41 |
Correct |
5 ms |
5228 KB |
Output is correct |
42 |
Correct |
5 ms |
5100 KB |
Output is correct |
43 |
Correct |
5 ms |
5100 KB |
Output is correct |
44 |
Correct |
5 ms |
5100 KB |
Output is correct |
45 |
Correct |
5 ms |
5100 KB |
Output is correct |
46 |
Correct |
6 ms |
5100 KB |
Output is correct |
47 |
Correct |
5 ms |
5108 KB |
Output is correct |
48 |
Correct |
5 ms |
5228 KB |
Output is correct |
49 |
Correct |
4 ms |
5228 KB |
Output is correct |
50 |
Correct |
5 ms |
5100 KB |
Output is correct |
51 |
Correct |
5 ms |
5100 KB |
Output is correct |
52 |
Correct |
5 ms |
5100 KB |
Output is correct |
53 |
Correct |
5 ms |
5100 KB |
Output is correct |
54 |
Correct |
5 ms |
5100 KB |
Output is correct |
55 |
Correct |
5 ms |
5100 KB |
Output is correct |
56 |
Correct |
4 ms |
5100 KB |
Output is correct |
57 |
Correct |
5 ms |
5228 KB |
Output is correct |
58 |
Correct |
9 ms |
5100 KB |
Output is correct |
59 |
Correct |
4 ms |
5100 KB |
Output is correct |
60 |
Correct |
5 ms |
5228 KB |
Output is correct |
61 |
Correct |
5 ms |
5100 KB |
Output is correct |
62 |
Correct |
5 ms |
5228 KB |
Output is correct |
63 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
5 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5116 KB |
Output is correct |
19 |
Correct |
4 ms |
5228 KB |
Output is correct |
20 |
Correct |
4 ms |
5116 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5100 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5100 KB |
Output is correct |
25 |
Correct |
5 ms |
5100 KB |
Output is correct |
26 |
Correct |
5 ms |
5100 KB |
Output is correct |
27 |
Correct |
5 ms |
5100 KB |
Output is correct |
28 |
Correct |
7 ms |
5100 KB |
Output is correct |
29 |
Correct |
6 ms |
5228 KB |
Output is correct |
30 |
Correct |
5 ms |
5228 KB |
Output is correct |
31 |
Correct |
6 ms |
5100 KB |
Output is correct |
32 |
Correct |
5 ms |
5100 KB |
Output is correct |
33 |
Correct |
4 ms |
5100 KB |
Output is correct |
34 |
Correct |
4 ms |
5100 KB |
Output is correct |
35 |
Correct |
4 ms |
5100 KB |
Output is correct |
36 |
Correct |
5 ms |
5100 KB |
Output is correct |
37 |
Correct |
5 ms |
5100 KB |
Output is correct |
38 |
Correct |
5 ms |
5100 KB |
Output is correct |
39 |
Correct |
5 ms |
5228 KB |
Output is correct |
40 |
Correct |
5 ms |
5100 KB |
Output is correct |
41 |
Correct |
5 ms |
5228 KB |
Output is correct |
42 |
Correct |
5 ms |
5100 KB |
Output is correct |
43 |
Correct |
5 ms |
5100 KB |
Output is correct |
44 |
Correct |
5 ms |
5100 KB |
Output is correct |
45 |
Correct |
5 ms |
5100 KB |
Output is correct |
46 |
Correct |
6 ms |
5100 KB |
Output is correct |
47 |
Correct |
5 ms |
5108 KB |
Output is correct |
48 |
Correct |
5 ms |
5228 KB |
Output is correct |
49 |
Correct |
4 ms |
5228 KB |
Output is correct |
50 |
Correct |
5 ms |
5100 KB |
Output is correct |
51 |
Correct |
5 ms |
5100 KB |
Output is correct |
52 |
Correct |
5 ms |
5100 KB |
Output is correct |
53 |
Correct |
5 ms |
5100 KB |
Output is correct |
54 |
Correct |
5 ms |
5100 KB |
Output is correct |
55 |
Correct |
5 ms |
5100 KB |
Output is correct |
56 |
Correct |
4 ms |
5100 KB |
Output is correct |
57 |
Correct |
5 ms |
5228 KB |
Output is correct |
58 |
Correct |
9 ms |
5100 KB |
Output is correct |
59 |
Correct |
4 ms |
5100 KB |
Output is correct |
60 |
Correct |
5 ms |
5228 KB |
Output is correct |
61 |
Correct |
5 ms |
5100 KB |
Output is correct |
62 |
Correct |
5 ms |
5228 KB |
Output is correct |
63 |
Correct |
4 ms |
5100 KB |
Output is correct |
64 |
Correct |
189 ms |
16016 KB |
Output is correct |
65 |
Correct |
183 ms |
16044 KB |
Output is correct |
66 |
Correct |
173 ms |
16024 KB |
Output is correct |
67 |
Correct |
179 ms |
16044 KB |
Output is correct |
68 |
Correct |
165 ms |
16024 KB |
Output is correct |
69 |
Correct |
166 ms |
16040 KB |
Output is correct |
70 |
Correct |
190 ms |
16060 KB |
Output is correct |
71 |
Correct |
169 ms |
16056 KB |
Output is correct |
72 |
Correct |
180 ms |
16168 KB |
Output is correct |
73 |
Correct |
199 ms |
28844 KB |
Output is correct |
74 |
Correct |
202 ms |
28528 KB |
Output is correct |
75 |
Correct |
164 ms |
16040 KB |
Output is correct |
76 |
Correct |
170 ms |
16048 KB |
Output is correct |
77 |
Correct |
189 ms |
16040 KB |
Output is correct |
78 |
Correct |
179 ms |
16004 KB |
Output is correct |
79 |
Correct |
176 ms |
15920 KB |
Output is correct |
80 |
Correct |
180 ms |
16040 KB |
Output is correct |
81 |
Correct |
164 ms |
16312 KB |
Output is correct |
82 |
Correct |
169 ms |
16108 KB |
Output is correct |
83 |
Correct |
180 ms |
21168 KB |
Output is correct |
84 |
Correct |
179 ms |
23224 KB |
Output is correct |
85 |
Correct |
179 ms |
22968 KB |
Output is correct |
86 |
Correct |
186 ms |
17452 KB |
Output is correct |
87 |
Correct |
173 ms |
16808 KB |
Output is correct |
88 |
Correct |
161 ms |
17256 KB |
Output is correct |
89 |
Correct |
168 ms |
17208 KB |
Output is correct |
90 |
Correct |
167 ms |
17036 KB |
Output is correct |
91 |
Correct |
177 ms |
17424 KB |
Output is correct |
92 |
Correct |
161 ms |
17560 KB |
Output is correct |
93 |
Correct |
156 ms |
19964 KB |
Output is correct |
94 |
Correct |
158 ms |
21732 KB |
Output is correct |
95 |
Correct |
164 ms |
20452 KB |
Output is correct |
96 |
Correct |
173 ms |
18584 KB |
Output is correct |
97 |
Correct |
157 ms |
17156 KB |
Output is correct |
98 |
Correct |
150 ms |
16604 KB |
Output is correct |
99 |
Correct |
172 ms |
16424 KB |
Output is correct |
100 |
Correct |
157 ms |
19736 KB |
Output is correct |
101 |
Correct |
166 ms |
17640 KB |
Output is correct |
102 |
Correct |
145 ms |
16764 KB |
Output is correct |
103 |
Correct |
160 ms |
16588 KB |
Output is correct |
104 |
Correct |
112 ms |
16864 KB |
Output is correct |
105 |
Correct |
198 ms |
25492 KB |
Output is correct |
106 |
Correct |
111 ms |
16736 KB |
Output is correct |
107 |
Correct |
112 ms |
16736 KB |
Output is correct |
108 |
Correct |
205 ms |
29356 KB |
Output is correct |
109 |
Correct |
121 ms |
16956 KB |
Output is correct |
110 |
Correct |
194 ms |
29740 KB |
Output is correct |
111 |
Correct |
183 ms |
16056 KB |
Output is correct |