#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 200000;
int n;
std::vector<int> e[N];
int a[N], dp[N];
void dfs(int u, int p) {
int sum = 0;
for (int v : e[u]) {
if (v != p) {
dfs(v, u);
sum += dp[v];
}
}
dp[u] = std::max(a[u], sum - a[u]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
std::string S;
std::cin >> S;
for (int i = 0; i < n; i++) {
a[i] = S[i] - '0';
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j : e[i]) {
ans = std::max(ans, a[i] + a[j]);
}
}
for (int i = 0; i < n; i++) {
dfs(i, -1);
ans = std::max(ans, dp[i]);
}
std::cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
2 ms |
4928 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
2 ms |
4928 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
61 ms |
5108 KB |
Output is correct |
23 |
Correct |
62 ms |
5068 KB |
Output is correct |
24 |
Correct |
62 ms |
5108 KB |
Output is correct |
25 |
Correct |
62 ms |
5068 KB |
Output is correct |
26 |
Correct |
61 ms |
5068 KB |
Output is correct |
27 |
Correct |
64 ms |
5060 KB |
Output is correct |
28 |
Correct |
63 ms |
5032 KB |
Output is correct |
29 |
Correct |
76 ms |
5160 KB |
Output is correct |
30 |
Correct |
77 ms |
5252 KB |
Output is correct |
31 |
Correct |
53 ms |
5068 KB |
Output is correct |
32 |
Correct |
54 ms |
5068 KB |
Output is correct |
33 |
Correct |
65 ms |
5104 KB |
Output is correct |
34 |
Correct |
63 ms |
5020 KB |
Output is correct |
35 |
Correct |
64 ms |
5104 KB |
Output is correct |
36 |
Correct |
65 ms |
5104 KB |
Output is correct |
37 |
Correct |
51 ms |
5108 KB |
Output is correct |
38 |
Correct |
58 ms |
5036 KB |
Output is correct |
39 |
Correct |
67 ms |
5068 KB |
Output is correct |
40 |
Correct |
69 ms |
5196 KB |
Output is correct |
41 |
Correct |
72 ms |
5176 KB |
Output is correct |
42 |
Correct |
65 ms |
5136 KB |
Output is correct |
43 |
Correct |
66 ms |
5068 KB |
Output is correct |
44 |
Correct |
53 ms |
5148 KB |
Output is correct |
45 |
Correct |
53 ms |
5068 KB |
Output is correct |
46 |
Correct |
54 ms |
5152 KB |
Output is correct |
47 |
Correct |
52 ms |
5068 KB |
Output is correct |
48 |
Correct |
53 ms |
5180 KB |
Output is correct |
49 |
Correct |
51 ms |
5204 KB |
Output is correct |
50 |
Correct |
52 ms |
5160 KB |
Output is correct |
51 |
Correct |
52 ms |
5144 KB |
Output is correct |
52 |
Correct |
56 ms |
5068 KB |
Output is correct |
53 |
Correct |
53 ms |
5068 KB |
Output is correct |
54 |
Correct |
53 ms |
5040 KB |
Output is correct |
55 |
Correct |
58 ms |
5124 KB |
Output is correct |
56 |
Correct |
33 ms |
5188 KB |
Output is correct |
57 |
Correct |
77 ms |
5256 KB |
Output is correct |
58 |
Correct |
27 ms |
5068 KB |
Output is correct |
59 |
Correct |
27 ms |
5144 KB |
Output is correct |
60 |
Correct |
76 ms |
5196 KB |
Output is correct |
61 |
Correct |
27 ms |
5068 KB |
Output is correct |
62 |
Correct |
77 ms |
5256 KB |
Output is correct |
63 |
Correct |
48 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
2 ms |
4928 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
61 ms |
5108 KB |
Output is correct |
23 |
Correct |
62 ms |
5068 KB |
Output is correct |
24 |
Correct |
62 ms |
5108 KB |
Output is correct |
25 |
Correct |
62 ms |
5068 KB |
Output is correct |
26 |
Correct |
61 ms |
5068 KB |
Output is correct |
27 |
Correct |
64 ms |
5060 KB |
Output is correct |
28 |
Correct |
63 ms |
5032 KB |
Output is correct |
29 |
Correct |
76 ms |
5160 KB |
Output is correct |
30 |
Correct |
77 ms |
5252 KB |
Output is correct |
31 |
Correct |
53 ms |
5068 KB |
Output is correct |
32 |
Correct |
54 ms |
5068 KB |
Output is correct |
33 |
Correct |
65 ms |
5104 KB |
Output is correct |
34 |
Correct |
63 ms |
5020 KB |
Output is correct |
35 |
Correct |
64 ms |
5104 KB |
Output is correct |
36 |
Correct |
65 ms |
5104 KB |
Output is correct |
37 |
Correct |
51 ms |
5108 KB |
Output is correct |
38 |
Correct |
58 ms |
5036 KB |
Output is correct |
39 |
Correct |
67 ms |
5068 KB |
Output is correct |
40 |
Correct |
69 ms |
5196 KB |
Output is correct |
41 |
Correct |
72 ms |
5176 KB |
Output is correct |
42 |
Correct |
65 ms |
5136 KB |
Output is correct |
43 |
Correct |
66 ms |
5068 KB |
Output is correct |
44 |
Correct |
53 ms |
5148 KB |
Output is correct |
45 |
Correct |
53 ms |
5068 KB |
Output is correct |
46 |
Correct |
54 ms |
5152 KB |
Output is correct |
47 |
Correct |
52 ms |
5068 KB |
Output is correct |
48 |
Correct |
53 ms |
5180 KB |
Output is correct |
49 |
Correct |
51 ms |
5204 KB |
Output is correct |
50 |
Correct |
52 ms |
5160 KB |
Output is correct |
51 |
Correct |
52 ms |
5144 KB |
Output is correct |
52 |
Correct |
56 ms |
5068 KB |
Output is correct |
53 |
Correct |
53 ms |
5068 KB |
Output is correct |
54 |
Correct |
53 ms |
5040 KB |
Output is correct |
55 |
Correct |
58 ms |
5124 KB |
Output is correct |
56 |
Correct |
33 ms |
5188 KB |
Output is correct |
57 |
Correct |
77 ms |
5256 KB |
Output is correct |
58 |
Correct |
27 ms |
5068 KB |
Output is correct |
59 |
Correct |
27 ms |
5144 KB |
Output is correct |
60 |
Correct |
76 ms |
5196 KB |
Output is correct |
61 |
Correct |
27 ms |
5068 KB |
Output is correct |
62 |
Correct |
77 ms |
5256 KB |
Output is correct |
63 |
Correct |
48 ms |
5068 KB |
Output is correct |
64 |
Execution timed out |
1550 ms |
15988 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |