#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
using u32 = uint32_t;
int main() {
u32 n, k;
std::cin >> n >> k;
std::vector<std::vector<u32>> g(n);
for (u32 i = 0; i < n - 1; ++i) {
u32 a, b;
std::cin >> a >> b;
--a, --b;
g[a].push_back(b);
g[b].push_back(a);
}
std::vector<std::vector<u32>> l(k);
for (u32 i = 0; i < n; ++i) {
u32 s;
std::cin >> s;
--s;
l[s].push_back(i);
}
std::vector<u32> d(n);
std::vector<u32> par(n);
std::function<void(u32, u32, u32)> const dfs = [&](u32 v, u32 p, u32 l) {
d[v] = l;
par[v] = p;
for (auto u : g[v])
if (u != p) dfs(u, v, l + 1);
};
dfs(0, 0, 0);
std::vector<u32> uf(n);
iota(uf.begin(), uf.end(), 0);
std::function<u32(u32)> const find = [&](u32 i) {
return uf[i] == i ? i : uf[i] = find(uf[i]);
};
auto const merge = [&](u32 i, u32 j) {
i = find(i);
j = find(j);
if (i == j) return;
if (d[i] > d[j]) std::swap(i, j);
uf[j] = i;
};
for (auto &m : l) {
std::set<std::pair<u32, u32>> s;
for (auto x : m) {
x = find(x);
s.emplace(d[x], x);
}
while (s.size() > 1) {
auto [_, v] = *s.rbegin();
s.erase(prev(s.end()));
merge(v, par[v]);
v = find(v);
s.emplace(d[v], v);
}
}
std::vector<u32> cnt(n);
for (u32 i = 1; i < n; ++i)
if (find(i) == i) {
auto j = find(par[i]);
cnt[i] += 1;
cnt[j] += 1;
}
auto x = count(cnt.begin(), cnt.end(), 1);
std::cout << (x + 1) / 2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
852 KB |
Output is correct |
29 |
Correct |
3 ms |
724 KB |
Output is correct |
30 |
Correct |
4 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
4 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
468 KB |
Output is correct |
37 |
Correct |
4 ms |
568 KB |
Output is correct |
38 |
Correct |
1 ms |
296 KB |
Output is correct |
39 |
Correct |
4 ms |
596 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
596 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
952 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
4 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
4 ms |
596 KB |
Output is correct |
49 |
Correct |
4 ms |
724 KB |
Output is correct |
50 |
Correct |
4 ms |
728 KB |
Output is correct |
51 |
Correct |
4 ms |
596 KB |
Output is correct |
52 |
Correct |
3 ms |
596 KB |
Output is correct |
53 |
Correct |
4 ms |
596 KB |
Output is correct |
54 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
300 KB |
Output is correct |
26 |
Correct |
138 ms |
9684 KB |
Output is correct |
27 |
Correct |
150 ms |
9412 KB |
Output is correct |
28 |
Correct |
4 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
138 ms |
9404 KB |
Output is correct |
32 |
Correct |
4 ms |
568 KB |
Output is correct |
33 |
Correct |
134 ms |
17984 KB |
Output is correct |
34 |
Correct |
159 ms |
9548 KB |
Output is correct |
35 |
Correct |
3 ms |
568 KB |
Output is correct |
36 |
Correct |
137 ms |
10248 KB |
Output is correct |
37 |
Correct |
3 ms |
596 KB |
Output is correct |
38 |
Correct |
4 ms |
588 KB |
Output is correct |
39 |
Correct |
127 ms |
9680 KB |
Output is correct |
40 |
Correct |
4 ms |
852 KB |
Output is correct |
41 |
Correct |
157 ms |
13500 KB |
Output is correct |
42 |
Correct |
138 ms |
12124 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
216 ms |
22476 KB |
Output is correct |
45 |
Correct |
131 ms |
13860 KB |
Output is correct |
46 |
Correct |
3 ms |
596 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
8228 KB |
Output is correct |
2 |
Correct |
126 ms |
13108 KB |
Output is correct |
3 |
Correct |
3 ms |
568 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
3 ms |
568 KB |
Output is correct |
8 |
Correct |
151 ms |
11852 KB |
Output is correct |
9 |
Correct |
4 ms |
596 KB |
Output is correct |
10 |
Correct |
144 ms |
9536 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
127 ms |
9556 KB |
Output is correct |
13 |
Correct |
126 ms |
11756 KB |
Output is correct |
14 |
Correct |
147 ms |
14652 KB |
Output is correct |
15 |
Correct |
111 ms |
9636 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
134 ms |
14324 KB |
Output is correct |
19 |
Correct |
147 ms |
19288 KB |
Output is correct |
20 |
Correct |
4 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
126 ms |
12168 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
128 ms |
10120 KB |
Output is correct |
25 |
Correct |
135 ms |
17768 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
4 ms |
724 KB |
Output is correct |
28 |
Correct |
4 ms |
592 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
852 KB |
Output is correct |
29 |
Correct |
3 ms |
724 KB |
Output is correct |
30 |
Correct |
4 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
4 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
468 KB |
Output is correct |
37 |
Correct |
4 ms |
568 KB |
Output is correct |
38 |
Correct |
1 ms |
296 KB |
Output is correct |
39 |
Correct |
4 ms |
596 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
596 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
952 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
4 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
4 ms |
596 KB |
Output is correct |
49 |
Correct |
4 ms |
724 KB |
Output is correct |
50 |
Correct |
4 ms |
728 KB |
Output is correct |
51 |
Correct |
4 ms |
596 KB |
Output is correct |
52 |
Correct |
3 ms |
596 KB |
Output is correct |
53 |
Correct |
4 ms |
596 KB |
Output is correct |
54 |
Correct |
4 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
300 KB |
Output is correct |
56 |
Correct |
138 ms |
9684 KB |
Output is correct |
57 |
Correct |
150 ms |
9412 KB |
Output is correct |
58 |
Correct |
4 ms |
600 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
138 ms |
9404 KB |
Output is correct |
62 |
Correct |
4 ms |
568 KB |
Output is correct |
63 |
Correct |
134 ms |
17984 KB |
Output is correct |
64 |
Correct |
159 ms |
9548 KB |
Output is correct |
65 |
Correct |
3 ms |
568 KB |
Output is correct |
66 |
Correct |
137 ms |
10248 KB |
Output is correct |
67 |
Correct |
3 ms |
596 KB |
Output is correct |
68 |
Correct |
4 ms |
588 KB |
Output is correct |
69 |
Correct |
127 ms |
9680 KB |
Output is correct |
70 |
Correct |
4 ms |
852 KB |
Output is correct |
71 |
Correct |
157 ms |
13500 KB |
Output is correct |
72 |
Correct |
138 ms |
12124 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
216 ms |
22476 KB |
Output is correct |
75 |
Correct |
131 ms |
13860 KB |
Output is correct |
76 |
Correct |
3 ms |
596 KB |
Output is correct |
77 |
Correct |
3 ms |
468 KB |
Output is correct |
78 |
Correct |
124 ms |
8228 KB |
Output is correct |
79 |
Correct |
126 ms |
13108 KB |
Output is correct |
80 |
Correct |
3 ms |
568 KB |
Output is correct |
81 |
Correct |
4 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
304 KB |
Output is correct |
84 |
Correct |
3 ms |
568 KB |
Output is correct |
85 |
Correct |
151 ms |
11852 KB |
Output is correct |
86 |
Correct |
4 ms |
596 KB |
Output is correct |
87 |
Correct |
144 ms |
9536 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
127 ms |
9556 KB |
Output is correct |
90 |
Correct |
126 ms |
11756 KB |
Output is correct |
91 |
Correct |
147 ms |
14652 KB |
Output is correct |
92 |
Correct |
111 ms |
9636 KB |
Output is correct |
93 |
Correct |
3 ms |
596 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
134 ms |
14324 KB |
Output is correct |
96 |
Correct |
147 ms |
19288 KB |
Output is correct |
97 |
Correct |
4 ms |
596 KB |
Output is correct |
98 |
Correct |
1 ms |
212 KB |
Output is correct |
99 |
Correct |
126 ms |
12168 KB |
Output is correct |
100 |
Correct |
4 ms |
596 KB |
Output is correct |
101 |
Correct |
128 ms |
10120 KB |
Output is correct |
102 |
Correct |
135 ms |
17768 KB |
Output is correct |
103 |
Correct |
3 ms |
724 KB |
Output is correct |
104 |
Correct |
4 ms |
724 KB |
Output is correct |
105 |
Correct |
4 ms |
592 KB |
Output is correct |
106 |
Correct |
3 ms |
596 KB |
Output is correct |
107 |
Correct |
4 ms |
596 KB |
Output is correct |
108 |
Correct |
825 ms |
58532 KB |
Output is correct |
109 |
Correct |
1315 ms |
77144 KB |
Output is correct |
110 |
Correct |
1245 ms |
102360 KB |
Output is correct |
111 |
Correct |
1040 ms |
101204 KB |
Output is correct |
112 |
Correct |
1070 ms |
89488 KB |
Output is correct |
113 |
Correct |
787 ms |
72116 KB |
Output is correct |
114 |
Correct |
1103 ms |
55328 KB |
Output is correct |
115 |
Correct |
1170 ms |
67192 KB |
Output is correct |
116 |
Correct |
1056 ms |
52924 KB |
Output is correct |
117 |
Correct |
988 ms |
73356 KB |
Output is correct |
118 |
Correct |
892 ms |
46884 KB |
Output is correct |
119 |
Correct |
915 ms |
73360 KB |
Output is correct |
120 |
Correct |
986 ms |
90200 KB |
Output is correct |
121 |
Correct |
1014 ms |
73364 KB |
Output is correct |
122 |
Correct |
984 ms |
62464 KB |
Output is correct |
123 |
Correct |
822 ms |
75044 KB |
Output is correct |
124 |
Correct |
1024 ms |
71828 KB |
Output is correct |