#include <iostream>
#include <vector>
using namespace std;
int f[200005][2], a[200005];
vector<int> g[200005];
void dfs(int x, int p) {
if (p != -1 && g[x].size() == 1) {
f[x][0] = f[x][1] = a[x];
return;
}
int c = 0, d = g[x].size() - (p != -1);
if (d > 1) d = 1;
else d = 0;
for (int w : g[x]) {
if (w == p) continue;
dfs(w, x);
c += f[w][1];
f[x][0] = max(f[x][0], f[w][0]);
f[x][0] = max(f[x][0], f[w][1] + a[x]);
}
f[x][1] = max(a[x], c - a[x]);
f[x][0] = max(f[x][0], c - a[x] * d);
//cout << x << " " << f[x][1] << " " << f[x][0] << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
string s;
cin >> s;
s = " " + s;
for (int i = 1; i <= n; i++) a[i] = (s[i] == '1');
dfs(1, -1);
cout << max(f[1][0], f[1][1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
4952 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
4952 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5196 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5340 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5208 KB |
Output is correct |
47 |
Correct |
2 ms |
5212 KB |
Output is correct |
48 |
Correct |
2 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
3 ms |
5212 KB |
Output is correct |
52 |
Correct |
2 ms |
5208 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
3 ms |
5212 KB |
Output is correct |
55 |
Correct |
3 ms |
5208 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
2 ms |
5260 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
4952 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5196 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5340 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5208 KB |
Output is correct |
47 |
Correct |
2 ms |
5212 KB |
Output is correct |
48 |
Correct |
2 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
3 ms |
5212 KB |
Output is correct |
52 |
Correct |
2 ms |
5208 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
3 ms |
5212 KB |
Output is correct |
55 |
Correct |
3 ms |
5208 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
3 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
2 ms |
5260 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5208 KB |
Output is correct |
64 |
Correct |
68 ms |
14292 KB |
Output is correct |
65 |
Correct |
59 ms |
16844 KB |
Output is correct |
66 |
Correct |
57 ms |
16908 KB |
Output is correct |
67 |
Correct |
57 ms |
16848 KB |
Output is correct |
68 |
Correct |
64 ms |
16828 KB |
Output is correct |
69 |
Correct |
65 ms |
16844 KB |
Output is correct |
70 |
Correct |
59 ms |
16864 KB |
Output is correct |
71 |
Correct |
57 ms |
16796 KB |
Output is correct |
72 |
Correct |
71 ms |
16840 KB |
Output is correct |
73 |
Correct |
76 ms |
31948 KB |
Output is correct |
74 |
Correct |
74 ms |
31948 KB |
Output is correct |
75 |
Correct |
55 ms |
16588 KB |
Output is correct |
76 |
Correct |
55 ms |
16600 KB |
Output is correct |
77 |
Correct |
58 ms |
16732 KB |
Output is correct |
78 |
Correct |
61 ms |
16592 KB |
Output is correct |
79 |
Correct |
56 ms |
16712 KB |
Output is correct |
80 |
Correct |
61 ms |
16588 KB |
Output is correct |
81 |
Correct |
54 ms |
17236 KB |
Output is correct |
82 |
Correct |
62 ms |
16780 KB |
Output is correct |
83 |
Correct |
83 ms |
22988 KB |
Output is correct |
84 |
Correct |
67 ms |
25564 KB |
Output is correct |
85 |
Correct |
70 ms |
25052 KB |
Output is correct |
86 |
Correct |
59 ms |
18380 KB |
Output is correct |
87 |
Correct |
58 ms |
17628 KB |
Output is correct |
88 |
Correct |
57 ms |
18172 KB |
Output is correct |
89 |
Correct |
84 ms |
18108 KB |
Output is correct |
90 |
Correct |
66 ms |
17792 KB |
Output is correct |
91 |
Correct |
57 ms |
18280 KB |
Output is correct |
92 |
Correct |
62 ms |
18616 KB |
Output is correct |
93 |
Correct |
56 ms |
21408 KB |
Output is correct |
94 |
Correct |
56 ms |
23496 KB |
Output is correct |
95 |
Correct |
65 ms |
21896 KB |
Output is correct |
96 |
Correct |
55 ms |
19772 KB |
Output is correct |
97 |
Correct |
59 ms |
17956 KB |
Output is correct |
98 |
Correct |
58 ms |
17404 KB |
Output is correct |
99 |
Correct |
60 ms |
17100 KB |
Output is correct |
100 |
Correct |
72 ms |
20980 KB |
Output is correct |
101 |
Correct |
56 ms |
18616 KB |
Output is correct |
102 |
Correct |
51 ms |
17568 KB |
Output is correct |
103 |
Correct |
56 ms |
17348 KB |
Output is correct |
104 |
Correct |
40 ms |
17608 KB |
Output is correct |
105 |
Correct |
68 ms |
28112 KB |
Output is correct |
106 |
Correct |
45 ms |
17604 KB |
Output is correct |
107 |
Correct |
40 ms |
17484 KB |
Output is correct |
108 |
Correct |
69 ms |
32716 KB |
Output is correct |
109 |
Correct |
46 ms |
17604 KB |
Output is correct |
110 |
Correct |
74 ms |
33168 KB |
Output is correct |
111 |
Correct |
54 ms |
16604 KB |
Output is correct |