#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pb push_back
constexpr int LIM = 2e5;
int dp[LIM + 10][2];
bool czy[LIM + 10];
int cnt[LIM + 10];
vi g[LIM + 10];
void cntdp(int v, int o) {
if(czy[v]) {
cnt[v] = 1;
}
int sum0 = 0, sum1 = 0;
int mx0 = 0, mx1 = 0;
for(auto x : g[v]) {
if(x != o) {
cntdp(x, v);
cnt[v] += cnt[x];
mx0 = max(mx0, dp[x][0]);
mx1 = max(mx1, dp[x][1]);
sum0 += dp[x][0];
sum1 += dp[x][1];
}
}
//dp[v][0] - nie wchodzi od gory
//dp[v][1] - wchodzi od gory
int ile = 0;
for(auto x : g[v]) {
if(x != o) {
if(cnt[v]) {
ile++;
}
}
}
if(czy[v]) {
dp[v][0] = max({mx1 + 1, mx0, sum1 - 1});
dp[v][1] = max(sum1 - 1, 1);
}
else {
dp[v][0] = max({mx0, sum1});
dp[v][1] = sum1;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
string tmp;
cin >> tmp;
for(int i = 1; i <= n; i++) {
if(tmp[i - 1] == '1') {
czy[i] = 1;
}
}
cntdp(1, 1);
cout << dp[1][0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5040 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 |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5036 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5164 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 |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5180 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5168 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 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 |
5188 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5144 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 |
5132 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5040 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 |
5040 KB |
Output is correct |
62 |
Correct |
3 ms |
5164 KB |
Output is correct |
63 |
Correct |
3 ms |
5036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5040 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 |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5036 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5164 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 |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5180 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5168 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 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 |
5188 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5144 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 |
5132 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5040 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 |
5040 KB |
Output is correct |
62 |
Correct |
3 ms |
5164 KB |
Output is correct |
63 |
Correct |
3 ms |
5036 KB |
Output is correct |
64 |
Correct |
73 ms |
16912 KB |
Output is correct |
65 |
Correct |
85 ms |
16968 KB |
Output is correct |
66 |
Correct |
87 ms |
16864 KB |
Output is correct |
67 |
Correct |
73 ms |
16932 KB |
Output is correct |
68 |
Correct |
106 ms |
16908 KB |
Output is correct |
69 |
Correct |
100 ms |
16900 KB |
Output is correct |
70 |
Correct |
74 ms |
16912 KB |
Output is correct |
71 |
Correct |
73 ms |
16900 KB |
Output is correct |
72 |
Correct |
74 ms |
16728 KB |
Output is correct |
73 |
Correct |
123 ms |
32084 KB |
Output is correct |
74 |
Correct |
93 ms |
32012 KB |
Output is correct |
75 |
Correct |
93 ms |
16856 KB |
Output is correct |
76 |
Correct |
82 ms |
16832 KB |
Output is correct |
77 |
Correct |
82 ms |
16776 KB |
Output is correct |
78 |
Correct |
78 ms |
16808 KB |
Output is correct |
79 |
Correct |
71 ms |
16800 KB |
Output is correct |
80 |
Correct |
65 ms |
16792 KB |
Output is correct |
81 |
Correct |
72 ms |
17300 KB |
Output is correct |
82 |
Correct |
68 ms |
16996 KB |
Output is correct |
83 |
Correct |
83 ms |
23052 KB |
Output is correct |
84 |
Correct |
95 ms |
25556 KB |
Output is correct |
85 |
Correct |
117 ms |
25196 KB |
Output is correct |
86 |
Correct |
77 ms |
18572 KB |
Output is correct |
87 |
Correct |
85 ms |
17868 KB |
Output is correct |
88 |
Correct |
71 ms |
18248 KB |
Output is correct |
89 |
Correct |
82 ms |
18296 KB |
Output is correct |
90 |
Correct |
81 ms |
18068 KB |
Output is correct |
91 |
Correct |
75 ms |
18284 KB |
Output is correct |
92 |
Correct |
72 ms |
18744 KB |
Output is correct |
93 |
Correct |
84 ms |
21668 KB |
Output is correct |
94 |
Correct |
69 ms |
23764 KB |
Output is correct |
95 |
Correct |
73 ms |
22072 KB |
Output is correct |
96 |
Correct |
72 ms |
20024 KB |
Output is correct |
97 |
Correct |
70 ms |
18160 KB |
Output is correct |
98 |
Correct |
70 ms |
17556 KB |
Output is correct |
99 |
Correct |
85 ms |
17236 KB |
Output is correct |
100 |
Correct |
81 ms |
21028 KB |
Output is correct |
101 |
Correct |
72 ms |
18624 KB |
Output is correct |
102 |
Correct |
64 ms |
17804 KB |
Output is correct |
103 |
Correct |
73 ms |
17544 KB |
Output is correct |
104 |
Correct |
47 ms |
16836 KB |
Output is correct |
105 |
Correct |
84 ms |
28076 KB |
Output is correct |
106 |
Correct |
49 ms |
17724 KB |
Output is correct |
107 |
Correct |
53 ms |
17668 KB |
Output is correct |
108 |
Correct |
101 ms |
32848 KB |
Output is correct |
109 |
Correct |
47 ms |
16876 KB |
Output is correct |
110 |
Correct |
95 ms |
33192 KB |
Output is correct |
111 |
Correct |
66 ms |
16804 KB |
Output is correct |