#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+5;
int n, k, ans;
vector<int> g[N], st[N];
vector<pair<int, int> > E;
int par[N][20], dep[N];
void init_lca(int u, int p) {
par[u][0] = p, dep[u] = dep[p] + 1;
for(int i = 1; i < 20; i++) par[u][i] = par[par[u][i-1]][i-1];
for(int v : g[u]) if(v != p) init_lca(v, u);
}
int lca(int a, int b) {
if(dep[a] < dep[b]) swap(a, b);
for(int i = 19; ~i; i--) if(dep[par[a][i]] >= dep[b]) a = par[a][i];
if(a == b) return a;
for(int i = 19; ~i; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
return par[a][0];
}
int dsu[N], deg[N];
int find(int x) { return dsu[x] = x == dsu[x] ? x : find(dsu[x]); }
int main() {
iota(dsu, dsu+N, 0);
scanf("%d %d", &n, &k);
for(int i = 1, a, b; i < n; i++) {
scanf("%d %d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
E.emplace_back(a, b);
}
init_lca(1, 0);
for(int i = 1, a; i <= n; i++) {
scanf("%d", &a);
st[a].emplace_back(i);
}
for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
int a = st[i][j-1], b = st[i][j];
int l = lca(a, b);
while(a != l) {
dsu[a] = find(par[a][0]);
a = par[a][0];
}
while(b != l) {
dsu[b] = find(par[b][0]);
b = par[b][0];
}
}
for(pair<int, int> p : E) {
int a, b; tie(a, b) = p;
if(find(a) != find(b)) ++deg[find(a)], ++deg[find(b)];
}
for(int i = 1; i <= n; i++) if(dsu[i] == i && deg[i] == 1) ++ans;
printf("%d\n", (ans + 1) / 2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:45:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
~~^~~~~~~~~~~~~~
mergers.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25720 KB |
Output is correct |
2 |
Correct |
26 ms |
25720 KB |
Output is correct |
3 |
Correct |
26 ms |
25848 KB |
Output is correct |
4 |
Correct |
26 ms |
25724 KB |
Output is correct |
5 |
Correct |
25 ms |
25820 KB |
Output is correct |
6 |
Correct |
25 ms |
25720 KB |
Output is correct |
7 |
Correct |
25 ms |
25720 KB |
Output is correct |
8 |
Correct |
26 ms |
25720 KB |
Output is correct |
9 |
Correct |
25 ms |
25724 KB |
Output is correct |
10 |
Correct |
25 ms |
25820 KB |
Output is correct |
11 |
Correct |
26 ms |
25800 KB |
Output is correct |
12 |
Correct |
26 ms |
25756 KB |
Output is correct |
13 |
Correct |
25 ms |
25696 KB |
Output is correct |
14 |
Correct |
26 ms |
25720 KB |
Output is correct |
15 |
Correct |
26 ms |
25720 KB |
Output is correct |
16 |
Correct |
26 ms |
25804 KB |
Output is correct |
17 |
Correct |
26 ms |
25800 KB |
Output is correct |
18 |
Correct |
25 ms |
25848 KB |
Output is correct |
19 |
Correct |
25 ms |
25796 KB |
Output is correct |
20 |
Correct |
25 ms |
25724 KB |
Output is correct |
21 |
Correct |
25 ms |
25820 KB |
Output is correct |
22 |
Correct |
25 ms |
25720 KB |
Output is correct |
23 |
Correct |
25 ms |
25720 KB |
Output is correct |
24 |
Correct |
25 ms |
25848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25720 KB |
Output is correct |
2 |
Correct |
26 ms |
25720 KB |
Output is correct |
3 |
Correct |
26 ms |
25848 KB |
Output is correct |
4 |
Correct |
26 ms |
25724 KB |
Output is correct |
5 |
Correct |
25 ms |
25820 KB |
Output is correct |
6 |
Correct |
25 ms |
25720 KB |
Output is correct |
7 |
Correct |
25 ms |
25720 KB |
Output is correct |
8 |
Correct |
26 ms |
25720 KB |
Output is correct |
9 |
Correct |
25 ms |
25724 KB |
Output is correct |
10 |
Correct |
25 ms |
25820 KB |
Output is correct |
11 |
Correct |
26 ms |
25800 KB |
Output is correct |
12 |
Correct |
26 ms |
25756 KB |
Output is correct |
13 |
Correct |
25 ms |
25696 KB |
Output is correct |
14 |
Correct |
26 ms |
25720 KB |
Output is correct |
15 |
Correct |
26 ms |
25720 KB |
Output is correct |
16 |
Correct |
26 ms |
25804 KB |
Output is correct |
17 |
Correct |
26 ms |
25800 KB |
Output is correct |
18 |
Correct |
25 ms |
25848 KB |
Output is correct |
19 |
Correct |
25 ms |
25796 KB |
Output is correct |
20 |
Correct |
25 ms |
25724 KB |
Output is correct |
21 |
Correct |
25 ms |
25820 KB |
Output is correct |
22 |
Correct |
25 ms |
25720 KB |
Output is correct |
23 |
Correct |
25 ms |
25720 KB |
Output is correct |
24 |
Correct |
25 ms |
25848 KB |
Output is correct |
25 |
Correct |
27 ms |
25892 KB |
Output is correct |
26 |
Correct |
29 ms |
26232 KB |
Output is correct |
27 |
Correct |
29 ms |
26232 KB |
Output is correct |
28 |
Correct |
29 ms |
26356 KB |
Output is correct |
29 |
Correct |
28 ms |
26232 KB |
Output is correct |
30 |
Correct |
29 ms |
26216 KB |
Output is correct |
31 |
Correct |
26 ms |
25720 KB |
Output is correct |
32 |
Correct |
28 ms |
26360 KB |
Output is correct |
33 |
Correct |
25 ms |
25720 KB |
Output is correct |
34 |
Correct |
27 ms |
26232 KB |
Output is correct |
35 |
Correct |
28 ms |
26360 KB |
Output is correct |
36 |
Correct |
28 ms |
26232 KB |
Output is correct |
37 |
Correct |
29 ms |
26236 KB |
Output is correct |
38 |
Correct |
26 ms |
25720 KB |
Output is correct |
39 |
Correct |
29 ms |
26232 KB |
Output is correct |
40 |
Correct |
29 ms |
26232 KB |
Output is correct |
41 |
Correct |
29 ms |
26232 KB |
Output is correct |
42 |
Correct |
28 ms |
26232 KB |
Output is correct |
43 |
Correct |
81 ms |
26360 KB |
Output is correct |
44 |
Correct |
30 ms |
25848 KB |
Output is correct |
45 |
Correct |
34 ms |
26492 KB |
Output is correct |
46 |
Correct |
29 ms |
26232 KB |
Output is correct |
47 |
Correct |
26 ms |
25848 KB |
Output is correct |
48 |
Correct |
29 ms |
26232 KB |
Output is correct |
49 |
Correct |
28 ms |
26360 KB |
Output is correct |
50 |
Correct |
31 ms |
26328 KB |
Output is correct |
51 |
Correct |
28 ms |
26360 KB |
Output is correct |
52 |
Correct |
28 ms |
26216 KB |
Output is correct |
53 |
Correct |
28 ms |
26232 KB |
Output is correct |
54 |
Correct |
34 ms |
26232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25720 KB |
Output is correct |
2 |
Correct |
26 ms |
25720 KB |
Output is correct |
3 |
Correct |
26 ms |
25848 KB |
Output is correct |
4 |
Correct |
26 ms |
25724 KB |
Output is correct |
5 |
Correct |
25 ms |
25820 KB |
Output is correct |
6 |
Correct |
25 ms |
25720 KB |
Output is correct |
7 |
Correct |
25 ms |
25720 KB |
Output is correct |
8 |
Correct |
26 ms |
25720 KB |
Output is correct |
9 |
Correct |
25 ms |
25724 KB |
Output is correct |
10 |
Correct |
25 ms |
25820 KB |
Output is correct |
11 |
Correct |
26 ms |
25800 KB |
Output is correct |
12 |
Correct |
26 ms |
25756 KB |
Output is correct |
13 |
Correct |
25 ms |
25696 KB |
Output is correct |
14 |
Correct |
26 ms |
25720 KB |
Output is correct |
15 |
Correct |
26 ms |
25720 KB |
Output is correct |
16 |
Correct |
26 ms |
25804 KB |
Output is correct |
17 |
Correct |
26 ms |
25800 KB |
Output is correct |
18 |
Correct |
25 ms |
25848 KB |
Output is correct |
19 |
Correct |
25 ms |
25796 KB |
Output is correct |
20 |
Correct |
25 ms |
25724 KB |
Output is correct |
21 |
Correct |
25 ms |
25820 KB |
Output is correct |
22 |
Correct |
25 ms |
25720 KB |
Output is correct |
23 |
Correct |
25 ms |
25720 KB |
Output is correct |
24 |
Correct |
25 ms |
25848 KB |
Output is correct |
25 |
Correct |
26 ms |
25848 KB |
Output is correct |
26 |
Correct |
137 ms |
39220 KB |
Output is correct |
27 |
Correct |
225 ms |
38924 KB |
Output is correct |
28 |
Correct |
29 ms |
26232 KB |
Output is correct |
29 |
Correct |
26 ms |
25848 KB |
Output is correct |
30 |
Correct |
26 ms |
25820 KB |
Output is correct |
31 |
Correct |
196 ms |
39144 KB |
Output is correct |
32 |
Correct |
33 ms |
26232 KB |
Output is correct |
33 |
Execution timed out |
3050 ms |
43212 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
38996 KB |
Output is correct |
2 |
Correct |
134 ms |
41988 KB |
Output is correct |
3 |
Correct |
28 ms |
26232 KB |
Output is correct |
4 |
Correct |
29 ms |
26232 KB |
Output is correct |
5 |
Correct |
26 ms |
25720 KB |
Output is correct |
6 |
Correct |
26 ms |
25720 KB |
Output is correct |
7 |
Correct |
32 ms |
26152 KB |
Output is correct |
8 |
Correct |
206 ms |
40396 KB |
Output is correct |
9 |
Correct |
35 ms |
26232 KB |
Output is correct |
10 |
Correct |
175 ms |
39528 KB |
Output is correct |
11 |
Correct |
27 ms |
25720 KB |
Output is correct |
12 |
Correct |
231 ms |
39336 KB |
Output is correct |
13 |
Correct |
192 ms |
40296 KB |
Output is correct |
14 |
Correct |
167 ms |
41948 KB |
Output is correct |
15 |
Correct |
151 ms |
39356 KB |
Output is correct |
16 |
Correct |
28 ms |
26232 KB |
Output is correct |
17 |
Correct |
25 ms |
25720 KB |
Output is correct |
18 |
Correct |
188 ms |
41896 KB |
Output is correct |
19 |
Correct |
190 ms |
44268 KB |
Output is correct |
20 |
Correct |
34 ms |
26236 KB |
Output is correct |
21 |
Correct |
25 ms |
25848 KB |
Output is correct |
22 |
Correct |
139 ms |
40632 KB |
Output is correct |
23 |
Correct |
29 ms |
26232 KB |
Output is correct |
24 |
Correct |
229 ms |
39632 KB |
Output is correct |
25 |
Correct |
169 ms |
43372 KB |
Output is correct |
26 |
Correct |
28 ms |
26360 KB |
Output is correct |
27 |
Correct |
28 ms |
26360 KB |
Output is correct |
28 |
Correct |
28 ms |
26280 KB |
Output is correct |
29 |
Correct |
28 ms |
26232 KB |
Output is correct |
30 |
Correct |
28 ms |
26232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25720 KB |
Output is correct |
2 |
Correct |
26 ms |
25720 KB |
Output is correct |
3 |
Correct |
26 ms |
25848 KB |
Output is correct |
4 |
Correct |
26 ms |
25724 KB |
Output is correct |
5 |
Correct |
25 ms |
25820 KB |
Output is correct |
6 |
Correct |
25 ms |
25720 KB |
Output is correct |
7 |
Correct |
25 ms |
25720 KB |
Output is correct |
8 |
Correct |
26 ms |
25720 KB |
Output is correct |
9 |
Correct |
25 ms |
25724 KB |
Output is correct |
10 |
Correct |
25 ms |
25820 KB |
Output is correct |
11 |
Correct |
26 ms |
25800 KB |
Output is correct |
12 |
Correct |
26 ms |
25756 KB |
Output is correct |
13 |
Correct |
25 ms |
25696 KB |
Output is correct |
14 |
Correct |
26 ms |
25720 KB |
Output is correct |
15 |
Correct |
26 ms |
25720 KB |
Output is correct |
16 |
Correct |
26 ms |
25804 KB |
Output is correct |
17 |
Correct |
26 ms |
25800 KB |
Output is correct |
18 |
Correct |
25 ms |
25848 KB |
Output is correct |
19 |
Correct |
25 ms |
25796 KB |
Output is correct |
20 |
Correct |
25 ms |
25724 KB |
Output is correct |
21 |
Correct |
25 ms |
25820 KB |
Output is correct |
22 |
Correct |
25 ms |
25720 KB |
Output is correct |
23 |
Correct |
25 ms |
25720 KB |
Output is correct |
24 |
Correct |
25 ms |
25848 KB |
Output is correct |
25 |
Correct |
27 ms |
25892 KB |
Output is correct |
26 |
Correct |
29 ms |
26232 KB |
Output is correct |
27 |
Correct |
29 ms |
26232 KB |
Output is correct |
28 |
Correct |
29 ms |
26356 KB |
Output is correct |
29 |
Correct |
28 ms |
26232 KB |
Output is correct |
30 |
Correct |
29 ms |
26216 KB |
Output is correct |
31 |
Correct |
26 ms |
25720 KB |
Output is correct |
32 |
Correct |
28 ms |
26360 KB |
Output is correct |
33 |
Correct |
25 ms |
25720 KB |
Output is correct |
34 |
Correct |
27 ms |
26232 KB |
Output is correct |
35 |
Correct |
28 ms |
26360 KB |
Output is correct |
36 |
Correct |
28 ms |
26232 KB |
Output is correct |
37 |
Correct |
29 ms |
26236 KB |
Output is correct |
38 |
Correct |
26 ms |
25720 KB |
Output is correct |
39 |
Correct |
29 ms |
26232 KB |
Output is correct |
40 |
Correct |
29 ms |
26232 KB |
Output is correct |
41 |
Correct |
29 ms |
26232 KB |
Output is correct |
42 |
Correct |
28 ms |
26232 KB |
Output is correct |
43 |
Correct |
81 ms |
26360 KB |
Output is correct |
44 |
Correct |
30 ms |
25848 KB |
Output is correct |
45 |
Correct |
34 ms |
26492 KB |
Output is correct |
46 |
Correct |
29 ms |
26232 KB |
Output is correct |
47 |
Correct |
26 ms |
25848 KB |
Output is correct |
48 |
Correct |
29 ms |
26232 KB |
Output is correct |
49 |
Correct |
28 ms |
26360 KB |
Output is correct |
50 |
Correct |
31 ms |
26328 KB |
Output is correct |
51 |
Correct |
28 ms |
26360 KB |
Output is correct |
52 |
Correct |
28 ms |
26216 KB |
Output is correct |
53 |
Correct |
28 ms |
26232 KB |
Output is correct |
54 |
Correct |
34 ms |
26232 KB |
Output is correct |
55 |
Correct |
26 ms |
25848 KB |
Output is correct |
56 |
Correct |
137 ms |
39220 KB |
Output is correct |
57 |
Correct |
225 ms |
38924 KB |
Output is correct |
58 |
Correct |
29 ms |
26232 KB |
Output is correct |
59 |
Correct |
26 ms |
25848 KB |
Output is correct |
60 |
Correct |
26 ms |
25820 KB |
Output is correct |
61 |
Correct |
196 ms |
39144 KB |
Output is correct |
62 |
Correct |
33 ms |
26232 KB |
Output is correct |
63 |
Execution timed out |
3050 ms |
43212 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |