#include <bits/stdc++.h>
#define fastio ios_base :: sync_with_stdio(0), cin.tie(0);
using namespace std;
using ll = long long;
const int mxN = 1e6 + 5, mod = 1e9 + 7;
int n, d[mxN], b[mxN], ans; string a;
vector <int> adj[mxN];
void calc(int v, int p = -1) {
for (auto to : adj[v]) {
if (to != p) {
calc(to, v);
d[v] += d[to];
}
}
if (d[v] > 0)
d[v] -= a[v - 1];
if (!d[v])
d[v] = a[v - 1];
}
void dfs(int v, int p = -1) {
int ret = 0, cnt = 0;
for (auto to : adj[v]) {
if (d[to] > 0)
++cnt;
ret += d[to];
}
if (cnt <= 1) ret += a[v - 1];
else {
if (ret > 0) ret -= a[v - 1];
if (!ret) ret = a[v - 1];
}
ans = max(ans, ret);
for (auto to : adj[v]) {
if (to != p) {
int dv = d[v], dto = d[to];
d[v] -= d[to];
if (d[v] <= 1) {
if (a[v - 1])
d[v] = 1;
}
if (d[to] == 1 && a[to - 1] == 1) d[to] = 0;
d[to] += d[v];
if (d[to] > 0) d[to] -= a[to - 1];
if (!d[to]) d[to] = a[to - 1];
dfs(to, v);
d[v] = dv, d[to] = dto;
}
}
}
int main() {
fastio;
cin >> n;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> a;
for (auto &x : a) x -= '0';
calc(1); dfs(1);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23808 KB |
Output is correct |
5 |
Correct |
12 ms |
23740 KB |
Output is correct |
6 |
Correct |
11 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23756 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23708 KB |
Output is correct |
12 |
Correct |
12 ms |
23756 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
12 ms |
23812 KB |
Output is correct |
15 |
Correct |
12 ms |
23688 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
11 ms |
23756 KB |
Output is correct |
18 |
Correct |
12 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23808 KB |
Output is correct |
5 |
Correct |
12 ms |
23740 KB |
Output is correct |
6 |
Correct |
11 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23756 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23708 KB |
Output is correct |
12 |
Correct |
12 ms |
23756 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
12 ms |
23812 KB |
Output is correct |
15 |
Correct |
12 ms |
23688 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
11 ms |
23756 KB |
Output is correct |
18 |
Correct |
12 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23796 KB |
Output is correct |
22 |
Correct |
13 ms |
23872 KB |
Output is correct |
23 |
Correct |
13 ms |
23884 KB |
Output is correct |
24 |
Correct |
12 ms |
23888 KB |
Output is correct |
25 |
Correct |
12 ms |
23824 KB |
Output is correct |
26 |
Correct |
12 ms |
23884 KB |
Output is correct |
27 |
Correct |
12 ms |
23884 KB |
Output is correct |
28 |
Correct |
12 ms |
23816 KB |
Output is correct |
29 |
Correct |
13 ms |
24012 KB |
Output is correct |
30 |
Correct |
15 ms |
23876 KB |
Output is correct |
31 |
Correct |
13 ms |
23884 KB |
Output is correct |
32 |
Correct |
12 ms |
23884 KB |
Output is correct |
33 |
Correct |
14 ms |
23884 KB |
Output is correct |
34 |
Correct |
16 ms |
23864 KB |
Output is correct |
35 |
Correct |
12 ms |
23884 KB |
Output is correct |
36 |
Correct |
13 ms |
23796 KB |
Output is correct |
37 |
Correct |
13 ms |
23852 KB |
Output is correct |
38 |
Correct |
12 ms |
23884 KB |
Output is correct |
39 |
Correct |
12 ms |
23920 KB |
Output is correct |
40 |
Correct |
12 ms |
23896 KB |
Output is correct |
41 |
Correct |
12 ms |
23884 KB |
Output is correct |
42 |
Correct |
13 ms |
23928 KB |
Output is correct |
43 |
Correct |
16 ms |
23820 KB |
Output is correct |
44 |
Correct |
14 ms |
23884 KB |
Output is correct |
45 |
Correct |
16 ms |
23884 KB |
Output is correct |
46 |
Correct |
18 ms |
23808 KB |
Output is correct |
47 |
Correct |
13 ms |
23884 KB |
Output is correct |
48 |
Correct |
15 ms |
23888 KB |
Output is correct |
49 |
Correct |
12 ms |
23884 KB |
Output is correct |
50 |
Correct |
12 ms |
23936 KB |
Output is correct |
51 |
Correct |
12 ms |
23884 KB |
Output is correct |
52 |
Correct |
12 ms |
23884 KB |
Output is correct |
53 |
Correct |
12 ms |
23820 KB |
Output is correct |
54 |
Correct |
12 ms |
23864 KB |
Output is correct |
55 |
Correct |
12 ms |
23884 KB |
Output is correct |
56 |
Correct |
12 ms |
23856 KB |
Output is correct |
57 |
Correct |
15 ms |
24012 KB |
Output is correct |
58 |
Correct |
13 ms |
23796 KB |
Output is correct |
59 |
Correct |
13 ms |
23884 KB |
Output is correct |
60 |
Correct |
13 ms |
23884 KB |
Output is correct |
61 |
Correct |
13 ms |
23904 KB |
Output is correct |
62 |
Correct |
14 ms |
24012 KB |
Output is correct |
63 |
Correct |
14 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
13 ms |
23808 KB |
Output is correct |
5 |
Correct |
12 ms |
23740 KB |
Output is correct |
6 |
Correct |
11 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23756 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
14 ms |
23708 KB |
Output is correct |
12 |
Correct |
12 ms |
23756 KB |
Output is correct |
13 |
Correct |
12 ms |
23756 KB |
Output is correct |
14 |
Correct |
12 ms |
23812 KB |
Output is correct |
15 |
Correct |
12 ms |
23688 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
11 ms |
23756 KB |
Output is correct |
18 |
Correct |
12 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23796 KB |
Output is correct |
22 |
Correct |
13 ms |
23872 KB |
Output is correct |
23 |
Correct |
13 ms |
23884 KB |
Output is correct |
24 |
Correct |
12 ms |
23888 KB |
Output is correct |
25 |
Correct |
12 ms |
23824 KB |
Output is correct |
26 |
Correct |
12 ms |
23884 KB |
Output is correct |
27 |
Correct |
12 ms |
23884 KB |
Output is correct |
28 |
Correct |
12 ms |
23816 KB |
Output is correct |
29 |
Correct |
13 ms |
24012 KB |
Output is correct |
30 |
Correct |
15 ms |
23876 KB |
Output is correct |
31 |
Correct |
13 ms |
23884 KB |
Output is correct |
32 |
Correct |
12 ms |
23884 KB |
Output is correct |
33 |
Correct |
14 ms |
23884 KB |
Output is correct |
34 |
Correct |
16 ms |
23864 KB |
Output is correct |
35 |
Correct |
12 ms |
23884 KB |
Output is correct |
36 |
Correct |
13 ms |
23796 KB |
Output is correct |
37 |
Correct |
13 ms |
23852 KB |
Output is correct |
38 |
Correct |
12 ms |
23884 KB |
Output is correct |
39 |
Correct |
12 ms |
23920 KB |
Output is correct |
40 |
Correct |
12 ms |
23896 KB |
Output is correct |
41 |
Correct |
12 ms |
23884 KB |
Output is correct |
42 |
Correct |
13 ms |
23928 KB |
Output is correct |
43 |
Correct |
16 ms |
23820 KB |
Output is correct |
44 |
Correct |
14 ms |
23884 KB |
Output is correct |
45 |
Correct |
16 ms |
23884 KB |
Output is correct |
46 |
Correct |
18 ms |
23808 KB |
Output is correct |
47 |
Correct |
13 ms |
23884 KB |
Output is correct |
48 |
Correct |
15 ms |
23888 KB |
Output is correct |
49 |
Correct |
12 ms |
23884 KB |
Output is correct |
50 |
Correct |
12 ms |
23936 KB |
Output is correct |
51 |
Correct |
12 ms |
23884 KB |
Output is correct |
52 |
Correct |
12 ms |
23884 KB |
Output is correct |
53 |
Correct |
12 ms |
23820 KB |
Output is correct |
54 |
Correct |
12 ms |
23864 KB |
Output is correct |
55 |
Correct |
12 ms |
23884 KB |
Output is correct |
56 |
Correct |
12 ms |
23856 KB |
Output is correct |
57 |
Correct |
15 ms |
24012 KB |
Output is correct |
58 |
Correct |
13 ms |
23796 KB |
Output is correct |
59 |
Correct |
13 ms |
23884 KB |
Output is correct |
60 |
Correct |
13 ms |
23884 KB |
Output is correct |
61 |
Correct |
13 ms |
23904 KB |
Output is correct |
62 |
Correct |
14 ms |
24012 KB |
Output is correct |
63 |
Correct |
14 ms |
23900 KB |
Output is correct |
64 |
Correct |
139 ms |
31292 KB |
Output is correct |
65 |
Correct |
158 ms |
34000 KB |
Output is correct |
66 |
Correct |
138 ms |
33908 KB |
Output is correct |
67 |
Correct |
170 ms |
34052 KB |
Output is correct |
68 |
Correct |
137 ms |
33964 KB |
Output is correct |
69 |
Correct |
172 ms |
34064 KB |
Output is correct |
70 |
Correct |
139 ms |
34000 KB |
Output is correct |
71 |
Correct |
145 ms |
33936 KB |
Output is correct |
72 |
Correct |
167 ms |
34256 KB |
Output is correct |
73 |
Correct |
173 ms |
49124 KB |
Output is correct |
74 |
Correct |
166 ms |
48980 KB |
Output is correct |
75 |
Correct |
142 ms |
33988 KB |
Output is correct |
76 |
Correct |
143 ms |
33788 KB |
Output is correct |
77 |
Correct |
142 ms |
33792 KB |
Output is correct |
78 |
Correct |
146 ms |
33984 KB |
Output is correct |
79 |
Correct |
150 ms |
33808 KB |
Output is correct |
80 |
Correct |
159 ms |
33836 KB |
Output is correct |
81 |
Correct |
125 ms |
34236 KB |
Output is correct |
82 |
Correct |
138 ms |
34172 KB |
Output is correct |
83 |
Correct |
152 ms |
40032 KB |
Output is correct |
84 |
Correct |
156 ms |
42632 KB |
Output is correct |
85 |
Correct |
169 ms |
42320 KB |
Output is correct |
86 |
Correct |
156 ms |
35664 KB |
Output is correct |
87 |
Correct |
170 ms |
34904 KB |
Output is correct |
88 |
Correct |
135 ms |
35264 KB |
Output is correct |
89 |
Correct |
141 ms |
35444 KB |
Output is correct |
90 |
Correct |
147 ms |
35044 KB |
Output is correct |
91 |
Correct |
136 ms |
35304 KB |
Output is correct |
92 |
Correct |
145 ms |
35840 KB |
Output is correct |
93 |
Correct |
138 ms |
38696 KB |
Output is correct |
94 |
Correct |
141 ms |
40748 KB |
Output is correct |
95 |
Correct |
129 ms |
39100 KB |
Output is correct |
96 |
Correct |
151 ms |
37000 KB |
Output is correct |
97 |
Correct |
136 ms |
35064 KB |
Output is correct |
98 |
Correct |
139 ms |
34532 KB |
Output is correct |
99 |
Correct |
129 ms |
34240 KB |
Output is correct |
100 |
Correct |
132 ms |
38128 KB |
Output is correct |
101 |
Correct |
151 ms |
35676 KB |
Output is correct |
102 |
Correct |
133 ms |
34820 KB |
Output is correct |
103 |
Correct |
158 ms |
34516 KB |
Output is correct |
104 |
Correct |
84 ms |
34744 KB |
Output is correct |
105 |
Correct |
168 ms |
45208 KB |
Output is correct |
106 |
Correct |
94 ms |
34652 KB |
Output is correct |
107 |
Correct |
86 ms |
34640 KB |
Output is correct |
108 |
Correct |
170 ms |
50084 KB |
Output is correct |
109 |
Correct |
85 ms |
34920 KB |
Output is correct |
110 |
Correct |
188 ms |
50412 KB |
Output is correct |
111 |
Correct |
129 ms |
33916 KB |
Output is correct |