#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+5;
int n, k, ans;
int qs[N], col[N], deg[N];
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];
}
void solve_qs(int u, int p) {
for(int v : g[u]) if(v != p) {
solve_qs(v, u);
qs[u] += qs[v];
}
}
void flood(int u, int c, int &cnt) {
col[u] = c;
for(int v : g[u]) {
if((!qs[v] && par[v][0] == u) || (par[u][0] == v && !qs[u])) {
++cnt;
continue;
}
if(!col[v]) flood(v, c, cnt);
}
}
int main() {
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 l = lca(st[i][j-1], st[i][j]);
++qs[st[i][j-1]];
++qs[st[i][j]];
qs[l] -= 2;
}
solve_qs(1, 0);
for(int i = 1, ptr = 0; i <= n; i++) if(!col[i]) {
int cnt = 0;
flood(i, ++ptr, cnt);
if(cnt == 1) ++ans;
}
printf("%d\n", (ans + 1) / 2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:59: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:47: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:49: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:56: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 |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
28 ms |
23800 KB |
Output is correct |
12 |
Correct |
29 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23812 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23820 KB |
Output is correct |
19 |
Correct |
24 ms |
23772 KB |
Output is correct |
20 |
Correct |
24 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
24 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
28 ms |
23800 KB |
Output is correct |
12 |
Correct |
29 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23812 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23820 KB |
Output is correct |
19 |
Correct |
24 ms |
23772 KB |
Output is correct |
20 |
Correct |
24 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
24 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23928 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24284 KB |
Output is correct |
28 |
Correct |
26 ms |
24432 KB |
Output is correct |
29 |
Correct |
26 ms |
24252 KB |
Output is correct |
30 |
Correct |
27 ms |
24312 KB |
Output is correct |
31 |
Correct |
24 ms |
23928 KB |
Output is correct |
32 |
Correct |
28 ms |
24440 KB |
Output is correct |
33 |
Correct |
25 ms |
23800 KB |
Output is correct |
34 |
Correct |
26 ms |
24312 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
26 ms |
24312 KB |
Output is correct |
37 |
Correct |
26 ms |
24312 KB |
Output is correct |
38 |
Correct |
23 ms |
23928 KB |
Output is correct |
39 |
Correct |
27 ms |
24312 KB |
Output is correct |
40 |
Correct |
26 ms |
24184 KB |
Output is correct |
41 |
Correct |
26 ms |
24184 KB |
Output is correct |
42 |
Correct |
26 ms |
24312 KB |
Output is correct |
43 |
Correct |
27 ms |
24444 KB |
Output is correct |
44 |
Correct |
23 ms |
23928 KB |
Output is correct |
45 |
Correct |
26 ms |
24284 KB |
Output is correct |
46 |
Correct |
26 ms |
24276 KB |
Output is correct |
47 |
Correct |
23 ms |
23800 KB |
Output is correct |
48 |
Correct |
26 ms |
24184 KB |
Output is correct |
49 |
Correct |
26 ms |
24312 KB |
Output is correct |
50 |
Correct |
26 ms |
24312 KB |
Output is correct |
51 |
Correct |
27 ms |
24312 KB |
Output is correct |
52 |
Correct |
26 ms |
24312 KB |
Output is correct |
53 |
Correct |
27 ms |
24312 KB |
Output is correct |
54 |
Correct |
27 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
28 ms |
23800 KB |
Output is correct |
12 |
Correct |
29 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23812 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23820 KB |
Output is correct |
19 |
Correct |
24 ms |
23772 KB |
Output is correct |
20 |
Correct |
24 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
24 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23928 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
132 ms |
37732 KB |
Output is correct |
27 |
Correct |
188 ms |
37524 KB |
Output is correct |
28 |
Correct |
26 ms |
24204 KB |
Output is correct |
29 |
Correct |
23 ms |
23800 KB |
Output is correct |
30 |
Correct |
23 ms |
23800 KB |
Output is correct |
31 |
Correct |
172 ms |
37288 KB |
Output is correct |
32 |
Correct |
26 ms |
24368 KB |
Output is correct |
33 |
Correct |
279 ms |
44544 KB |
Output is correct |
34 |
Correct |
182 ms |
37440 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
211 ms |
37868 KB |
Output is correct |
37 |
Correct |
27 ms |
24188 KB |
Output is correct |
38 |
Correct |
27 ms |
24312 KB |
Output is correct |
39 |
Correct |
139 ms |
37736 KB |
Output is correct |
40 |
Correct |
27 ms |
24440 KB |
Output is correct |
41 |
Correct |
178 ms |
37788 KB |
Output is correct |
42 |
Correct |
262 ms |
40528 KB |
Output is correct |
43 |
Correct |
23 ms |
23804 KB |
Output is correct |
44 |
Correct |
291 ms |
45324 KB |
Output is correct |
45 |
Correct |
237 ms |
41192 KB |
Output is correct |
46 |
Correct |
26 ms |
24312 KB |
Output is correct |
47 |
Correct |
26 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
37740 KB |
Output is correct |
2 |
Correct |
136 ms |
39908 KB |
Output is correct |
3 |
Correct |
27 ms |
24312 KB |
Output is correct |
4 |
Correct |
27 ms |
24300 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
31 ms |
24312 KB |
Output is correct |
8 |
Correct |
191 ms |
38504 KB |
Output is correct |
9 |
Correct |
26 ms |
24312 KB |
Output is correct |
10 |
Correct |
167 ms |
37484 KB |
Output is correct |
11 |
Correct |
23 ms |
23928 KB |
Output is correct |
12 |
Correct |
202 ms |
37324 KB |
Output is correct |
13 |
Correct |
181 ms |
38508 KB |
Output is correct |
14 |
Correct |
162 ms |
40044 KB |
Output is correct |
15 |
Correct |
164 ms |
37736 KB |
Output is correct |
16 |
Correct |
26 ms |
24316 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
146 ms |
39916 KB |
Output is correct |
19 |
Correct |
186 ms |
43176 KB |
Output is correct |
20 |
Correct |
26 ms |
24312 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
145 ms |
38728 KB |
Output is correct |
23 |
Correct |
26 ms |
24312 KB |
Output is correct |
24 |
Correct |
196 ms |
37740 KB |
Output is correct |
25 |
Correct |
171 ms |
42112 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24312 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
26 ms |
24312 KB |
Output is correct |
30 |
Correct |
26 ms |
24316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
28 ms |
23800 KB |
Output is correct |
12 |
Correct |
29 ms |
23800 KB |
Output is correct |
13 |
Correct |
25 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
23 ms |
23812 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23820 KB |
Output is correct |
19 |
Correct |
24 ms |
23772 KB |
Output is correct |
20 |
Correct |
24 ms |
23800 KB |
Output is correct |
21 |
Correct |
23 ms |
23804 KB |
Output is correct |
22 |
Correct |
24 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23928 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24284 KB |
Output is correct |
28 |
Correct |
26 ms |
24432 KB |
Output is correct |
29 |
Correct |
26 ms |
24252 KB |
Output is correct |
30 |
Correct |
27 ms |
24312 KB |
Output is correct |
31 |
Correct |
24 ms |
23928 KB |
Output is correct |
32 |
Correct |
28 ms |
24440 KB |
Output is correct |
33 |
Correct |
25 ms |
23800 KB |
Output is correct |
34 |
Correct |
26 ms |
24312 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
26 ms |
24312 KB |
Output is correct |
37 |
Correct |
26 ms |
24312 KB |
Output is correct |
38 |
Correct |
23 ms |
23928 KB |
Output is correct |
39 |
Correct |
27 ms |
24312 KB |
Output is correct |
40 |
Correct |
26 ms |
24184 KB |
Output is correct |
41 |
Correct |
26 ms |
24184 KB |
Output is correct |
42 |
Correct |
26 ms |
24312 KB |
Output is correct |
43 |
Correct |
27 ms |
24444 KB |
Output is correct |
44 |
Correct |
23 ms |
23928 KB |
Output is correct |
45 |
Correct |
26 ms |
24284 KB |
Output is correct |
46 |
Correct |
26 ms |
24276 KB |
Output is correct |
47 |
Correct |
23 ms |
23800 KB |
Output is correct |
48 |
Correct |
26 ms |
24184 KB |
Output is correct |
49 |
Correct |
26 ms |
24312 KB |
Output is correct |
50 |
Correct |
26 ms |
24312 KB |
Output is correct |
51 |
Correct |
27 ms |
24312 KB |
Output is correct |
52 |
Correct |
26 ms |
24312 KB |
Output is correct |
53 |
Correct |
27 ms |
24312 KB |
Output is correct |
54 |
Correct |
27 ms |
24184 KB |
Output is correct |
55 |
Correct |
23 ms |
23800 KB |
Output is correct |
56 |
Correct |
132 ms |
37732 KB |
Output is correct |
57 |
Correct |
188 ms |
37524 KB |
Output is correct |
58 |
Correct |
26 ms |
24204 KB |
Output is correct |
59 |
Correct |
23 ms |
23800 KB |
Output is correct |
60 |
Correct |
23 ms |
23800 KB |
Output is correct |
61 |
Correct |
172 ms |
37288 KB |
Output is correct |
62 |
Correct |
26 ms |
24368 KB |
Output is correct |
63 |
Correct |
279 ms |
44544 KB |
Output is correct |
64 |
Correct |
182 ms |
37440 KB |
Output is correct |
65 |
Correct |
26 ms |
24312 KB |
Output is correct |
66 |
Correct |
211 ms |
37868 KB |
Output is correct |
67 |
Correct |
27 ms |
24188 KB |
Output is correct |
68 |
Correct |
27 ms |
24312 KB |
Output is correct |
69 |
Correct |
139 ms |
37736 KB |
Output is correct |
70 |
Correct |
27 ms |
24440 KB |
Output is correct |
71 |
Correct |
178 ms |
37788 KB |
Output is correct |
72 |
Correct |
262 ms |
40528 KB |
Output is correct |
73 |
Correct |
23 ms |
23804 KB |
Output is correct |
74 |
Correct |
291 ms |
45324 KB |
Output is correct |
75 |
Correct |
237 ms |
41192 KB |
Output is correct |
76 |
Correct |
26 ms |
24312 KB |
Output is correct |
77 |
Correct |
26 ms |
24184 KB |
Output is correct |
78 |
Correct |
131 ms |
37740 KB |
Output is correct |
79 |
Correct |
136 ms |
39908 KB |
Output is correct |
80 |
Correct |
27 ms |
24312 KB |
Output is correct |
81 |
Correct |
27 ms |
24300 KB |
Output is correct |
82 |
Correct |
24 ms |
23928 KB |
Output is correct |
83 |
Correct |
23 ms |
23800 KB |
Output is correct |
84 |
Correct |
31 ms |
24312 KB |
Output is correct |
85 |
Correct |
191 ms |
38504 KB |
Output is correct |
86 |
Correct |
26 ms |
24312 KB |
Output is correct |
87 |
Correct |
167 ms |
37484 KB |
Output is correct |
88 |
Correct |
23 ms |
23928 KB |
Output is correct |
89 |
Correct |
202 ms |
37324 KB |
Output is correct |
90 |
Correct |
181 ms |
38508 KB |
Output is correct |
91 |
Correct |
162 ms |
40044 KB |
Output is correct |
92 |
Correct |
164 ms |
37736 KB |
Output is correct |
93 |
Correct |
26 ms |
24316 KB |
Output is correct |
94 |
Correct |
23 ms |
23928 KB |
Output is correct |
95 |
Correct |
146 ms |
39916 KB |
Output is correct |
96 |
Correct |
186 ms |
43176 KB |
Output is correct |
97 |
Correct |
26 ms |
24312 KB |
Output is correct |
98 |
Correct |
23 ms |
23800 KB |
Output is correct |
99 |
Correct |
145 ms |
38728 KB |
Output is correct |
100 |
Correct |
26 ms |
24312 KB |
Output is correct |
101 |
Correct |
196 ms |
37740 KB |
Output is correct |
102 |
Correct |
171 ms |
42112 KB |
Output is correct |
103 |
Correct |
26 ms |
24312 KB |
Output is correct |
104 |
Correct |
26 ms |
24312 KB |
Output is correct |
105 |
Correct |
26 ms |
24184 KB |
Output is correct |
106 |
Correct |
26 ms |
24312 KB |
Output is correct |
107 |
Correct |
26 ms |
24316 KB |
Output is correct |
108 |
Correct |
843 ms |
97028 KB |
Output is correct |
109 |
Correct |
2021 ms |
110640 KB |
Output is correct |
110 |
Correct |
2041 ms |
122300 KB |
Output is correct |
111 |
Correct |
1238 ms |
123016 KB |
Output is correct |
112 |
Correct |
1194 ms |
116572 KB |
Output is correct |
113 |
Correct |
851 ms |
104912 KB |
Output is correct |
114 |
Correct |
1100 ms |
93604 KB |
Output is correct |
115 |
Correct |
1107 ms |
94200 KB |
Output is correct |
116 |
Correct |
1286 ms |
95072 KB |
Output is correct |
117 |
Correct |
984 ms |
105880 KB |
Output is correct |
118 |
Correct |
1066 ms |
92208 KB |
Output is correct |
119 |
Correct |
976 ms |
105896 KB |
Output is correct |
120 |
Correct |
1203 ms |
117100 KB |
Output is correct |
121 |
Correct |
986 ms |
105948 KB |
Output is correct |
122 |
Correct |
1167 ms |
99732 KB |
Output is correct |
123 |
Correct |
714 ms |
105380 KB |
Output is correct |
124 |
Correct |
1097 ms |
104284 KB |
Output is correct |