#include <bits/stdc++.h>
using namespace std;
int N, K, X, Y, state[500005], par[500005], sz[500005], anc[500005];
int lca[500005][20], depth[500005];
vector<int> AdjList[500005], AdjList2[500005], st[500005];
void dfs_init(int x, int p){
anc[x] = p;
lca[x][0] = p;
for (int i = 1; i < 20; ++i){
lca[x][i] = lca[lca[x][i-1]][i-1];
}
for (auto it : AdjList[x]){
if (it == p) continue;
depth[it] = depth[x] + 1;
dfs_init(it,x);
}
}
int get_lca(int x, int y){
if (depth[x] > depth[y]) swap(x,y);
int d = depth[y] - depth[x];
for (int i = 0; i < 20; ++i){
if (d & (1 << i)){
y = lca[y][i];
}
}
if (x == y) return x;
for (int i = 19; i >= 0; --i){
if (lca[x][i] != lca[y][i]){
x = lca[x][i];
y = lca[y][i];
}
}
return lca[x][0];
}
int find_par(int x){
if (x != par[x]) par[x] = find_par(par[x]);
return par[x];
}
void merg(int x, int y){
int X = find_par(x), Y = find_par(y);
if (X == Y) return;
if (sz[X] < sz[Y]) swap(X,Y);
par[Y] = X;
sz[X] += sz[Y];
if (depth[anc[Y]] < depth[anc[X]]){
anc[X] = anc[Y];
}
}
void combine(int x, int t){
int X = find_par(x), T = find_par(t);
if (X == T) return;
combine(anc[X],T);
merg(X,anc[X]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i < N; ++i){
cin >> X >> Y;
AdjList[X].push_back(Y);
AdjList[Y].push_back(X);
}
for (int i = 1; i <= N; ++i){
par[i] = i;
sz[i] = 1;
cin >> state[i];
st[state[i]].push_back(i);
}
dfs_init(1,0);
merg(1,0);
for (int i = 1; i <= K; ++i){
int L = st[i][0];
for (int j = 1; j < st[i].size(); ++j){
L = get_lca(L,st[i][j]);
}
//cout << i << ' ' << L << '\n';
for (auto it : st[i]){
combine(it,L);
}
}
/*
for (int i = 1; i <= N; ++i){
cout << i << ' ' << find_par(i) << '\n';
}
cout << '\n';
*/
for (int i = 1; i <= N; ++i){
for (auto it : AdjList[i]){
if (find_par(i) != find_par(it)){
AdjList2[find_par(i)].push_back(find_par(it));
}
}
}
int leaves = 0;
for (int i = 0; i <= N; ++i){
if (AdjList2[i].size() == 1) leaves++;
}
cout << (leaves+1)/2 << '\n';
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:82:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j < st[i].size(); ++j){
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
35576 KB |
Output is correct |
2 |
Correct |
25 ms |
35576 KB |
Output is correct |
3 |
Correct |
26 ms |
35580 KB |
Output is correct |
4 |
Correct |
25 ms |
35576 KB |
Output is correct |
5 |
Correct |
26 ms |
35704 KB |
Output is correct |
6 |
Correct |
25 ms |
35576 KB |
Output is correct |
7 |
Correct |
25 ms |
35576 KB |
Output is correct |
8 |
Correct |
25 ms |
35576 KB |
Output is correct |
9 |
Correct |
26 ms |
35576 KB |
Output is correct |
10 |
Correct |
25 ms |
35576 KB |
Output is correct |
11 |
Correct |
26 ms |
35576 KB |
Output is correct |
12 |
Correct |
25 ms |
35576 KB |
Output is correct |
13 |
Correct |
25 ms |
35576 KB |
Output is correct |
14 |
Correct |
25 ms |
35576 KB |
Output is correct |
15 |
Correct |
26 ms |
35576 KB |
Output is correct |
16 |
Correct |
26 ms |
35576 KB |
Output is correct |
17 |
Correct |
26 ms |
35576 KB |
Output is correct |
18 |
Correct |
26 ms |
35576 KB |
Output is correct |
19 |
Correct |
26 ms |
35576 KB |
Output is correct |
20 |
Correct |
26 ms |
35576 KB |
Output is correct |
21 |
Correct |
27 ms |
35576 KB |
Output is correct |
22 |
Correct |
25 ms |
35576 KB |
Output is correct |
23 |
Correct |
26 ms |
35576 KB |
Output is correct |
24 |
Correct |
27 ms |
35576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
35576 KB |
Output is correct |
2 |
Correct |
25 ms |
35576 KB |
Output is correct |
3 |
Correct |
26 ms |
35580 KB |
Output is correct |
4 |
Correct |
25 ms |
35576 KB |
Output is correct |
5 |
Correct |
26 ms |
35704 KB |
Output is correct |
6 |
Correct |
25 ms |
35576 KB |
Output is correct |
7 |
Correct |
25 ms |
35576 KB |
Output is correct |
8 |
Correct |
25 ms |
35576 KB |
Output is correct |
9 |
Correct |
26 ms |
35576 KB |
Output is correct |
10 |
Correct |
25 ms |
35576 KB |
Output is correct |
11 |
Correct |
26 ms |
35576 KB |
Output is correct |
12 |
Correct |
25 ms |
35576 KB |
Output is correct |
13 |
Correct |
25 ms |
35576 KB |
Output is correct |
14 |
Correct |
25 ms |
35576 KB |
Output is correct |
15 |
Correct |
26 ms |
35576 KB |
Output is correct |
16 |
Correct |
26 ms |
35576 KB |
Output is correct |
17 |
Correct |
26 ms |
35576 KB |
Output is correct |
18 |
Correct |
26 ms |
35576 KB |
Output is correct |
19 |
Correct |
26 ms |
35576 KB |
Output is correct |
20 |
Correct |
26 ms |
35576 KB |
Output is correct |
21 |
Correct |
27 ms |
35576 KB |
Output is correct |
22 |
Correct |
25 ms |
35576 KB |
Output is correct |
23 |
Correct |
26 ms |
35576 KB |
Output is correct |
24 |
Correct |
27 ms |
35576 KB |
Output is correct |
25 |
Correct |
26 ms |
35576 KB |
Output is correct |
26 |
Correct |
28 ms |
36216 KB |
Output is correct |
27 |
Correct |
28 ms |
36088 KB |
Output is correct |
28 |
Correct |
28 ms |
36216 KB |
Output is correct |
29 |
Correct |
29 ms |
36216 KB |
Output is correct |
30 |
Correct |
27 ms |
36088 KB |
Output is correct |
31 |
Correct |
25 ms |
35576 KB |
Output is correct |
32 |
Correct |
27 ms |
36216 KB |
Output is correct |
33 |
Correct |
26 ms |
35576 KB |
Output is correct |
34 |
Correct |
27 ms |
36088 KB |
Output is correct |
35 |
Correct |
29 ms |
36216 KB |
Output is correct |
36 |
Correct |
28 ms |
36088 KB |
Output is correct |
37 |
Correct |
28 ms |
36088 KB |
Output is correct |
38 |
Correct |
27 ms |
35576 KB |
Output is correct |
39 |
Correct |
28 ms |
36088 KB |
Output is correct |
40 |
Correct |
34 ms |
36088 KB |
Output is correct |
41 |
Correct |
27 ms |
36088 KB |
Output is correct |
42 |
Correct |
27 ms |
36088 KB |
Output is correct |
43 |
Correct |
29 ms |
36164 KB |
Output is correct |
44 |
Correct |
25 ms |
35708 KB |
Output is correct |
45 |
Correct |
28 ms |
36088 KB |
Output is correct |
46 |
Correct |
27 ms |
36088 KB |
Output is correct |
47 |
Correct |
27 ms |
35576 KB |
Output is correct |
48 |
Correct |
28 ms |
36088 KB |
Output is correct |
49 |
Correct |
27 ms |
36216 KB |
Output is correct |
50 |
Correct |
29 ms |
36216 KB |
Output is correct |
51 |
Correct |
28 ms |
36088 KB |
Output is correct |
52 |
Correct |
27 ms |
36088 KB |
Output is correct |
53 |
Correct |
27 ms |
36088 KB |
Output is correct |
54 |
Correct |
28 ms |
36088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
35576 KB |
Output is correct |
2 |
Correct |
25 ms |
35576 KB |
Output is correct |
3 |
Correct |
26 ms |
35580 KB |
Output is correct |
4 |
Correct |
25 ms |
35576 KB |
Output is correct |
5 |
Correct |
26 ms |
35704 KB |
Output is correct |
6 |
Correct |
25 ms |
35576 KB |
Output is correct |
7 |
Correct |
25 ms |
35576 KB |
Output is correct |
8 |
Correct |
25 ms |
35576 KB |
Output is correct |
9 |
Correct |
26 ms |
35576 KB |
Output is correct |
10 |
Correct |
25 ms |
35576 KB |
Output is correct |
11 |
Correct |
26 ms |
35576 KB |
Output is correct |
12 |
Correct |
25 ms |
35576 KB |
Output is correct |
13 |
Correct |
25 ms |
35576 KB |
Output is correct |
14 |
Correct |
25 ms |
35576 KB |
Output is correct |
15 |
Correct |
26 ms |
35576 KB |
Output is correct |
16 |
Correct |
26 ms |
35576 KB |
Output is correct |
17 |
Correct |
26 ms |
35576 KB |
Output is correct |
18 |
Correct |
26 ms |
35576 KB |
Output is correct |
19 |
Correct |
26 ms |
35576 KB |
Output is correct |
20 |
Correct |
26 ms |
35576 KB |
Output is correct |
21 |
Correct |
27 ms |
35576 KB |
Output is correct |
22 |
Correct |
25 ms |
35576 KB |
Output is correct |
23 |
Correct |
26 ms |
35576 KB |
Output is correct |
24 |
Correct |
27 ms |
35576 KB |
Output is correct |
25 |
Correct |
25 ms |
35576 KB |
Output is correct |
26 |
Correct |
116 ms |
50900 KB |
Output is correct |
27 |
Correct |
145 ms |
50552 KB |
Output is correct |
28 |
Correct |
27 ms |
36088 KB |
Output is correct |
29 |
Correct |
25 ms |
35576 KB |
Output is correct |
30 |
Correct |
26 ms |
35576 KB |
Output is correct |
31 |
Correct |
139 ms |
50424 KB |
Output is correct |
32 |
Correct |
27 ms |
36088 KB |
Output is correct |
33 |
Correct |
176 ms |
54744 KB |
Output is correct |
34 |
Correct |
146 ms |
50616 KB |
Output is correct |
35 |
Correct |
28 ms |
36088 KB |
Output is correct |
36 |
Correct |
157 ms |
50808 KB |
Output is correct |
37 |
Correct |
27 ms |
36088 KB |
Output is correct |
38 |
Correct |
27 ms |
36088 KB |
Output is correct |
39 |
Correct |
112 ms |
50800 KB |
Output is correct |
40 |
Correct |
27 ms |
36216 KB |
Output is correct |
41 |
Correct |
139 ms |
50540 KB |
Output is correct |
42 |
Correct |
171 ms |
52136 KB |
Output is correct |
43 |
Correct |
26 ms |
35576 KB |
Output is correct |
44 |
Correct |
166 ms |
54896 KB |
Output is correct |
45 |
Correct |
184 ms |
52728 KB |
Output is correct |
46 |
Correct |
28 ms |
36088 KB |
Output is correct |
47 |
Correct |
28 ms |
36088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
49396 KB |
Output is correct |
2 |
Correct |
124 ms |
55532 KB |
Output is correct |
3 |
Correct |
34 ms |
36376 KB |
Output is correct |
4 |
Correct |
27 ms |
36088 KB |
Output is correct |
5 |
Correct |
25 ms |
35576 KB |
Output is correct |
6 |
Correct |
26 ms |
35580 KB |
Output is correct |
7 |
Correct |
27 ms |
36088 KB |
Output is correct |
8 |
Correct |
163 ms |
52344 KB |
Output is correct |
9 |
Correct |
27 ms |
36088 KB |
Output is correct |
10 |
Correct |
137 ms |
50680 KB |
Output is correct |
11 |
Correct |
25 ms |
35576 KB |
Output is correct |
12 |
Correct |
158 ms |
50680 KB |
Output is correct |
13 |
Correct |
164 ms |
52472 KB |
Output is correct |
14 |
Correct |
145 ms |
56696 KB |
Output is correct |
15 |
Correct |
114 ms |
50800 KB |
Output is correct |
16 |
Correct |
29 ms |
36092 KB |
Output is correct |
17 |
Correct |
26 ms |
35580 KB |
Output is correct |
18 |
Correct |
128 ms |
56300 KB |
Output is correct |
19 |
Correct |
168 ms |
59012 KB |
Output is correct |
20 |
Correct |
27 ms |
36092 KB |
Output is correct |
21 |
Correct |
25 ms |
35576 KB |
Output is correct |
22 |
Correct |
127 ms |
52720 KB |
Output is correct |
23 |
Correct |
27 ms |
36088 KB |
Output is correct |
24 |
Correct |
169 ms |
51320 KB |
Output is correct |
25 |
Correct |
169 ms |
58104 KB |
Output is correct |
26 |
Correct |
28 ms |
36216 KB |
Output is correct |
27 |
Correct |
27 ms |
36216 KB |
Output is correct |
28 |
Correct |
28 ms |
36088 KB |
Output is correct |
29 |
Correct |
27 ms |
36088 KB |
Output is correct |
30 |
Correct |
27 ms |
36088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
35576 KB |
Output is correct |
2 |
Correct |
25 ms |
35576 KB |
Output is correct |
3 |
Correct |
26 ms |
35580 KB |
Output is correct |
4 |
Correct |
25 ms |
35576 KB |
Output is correct |
5 |
Correct |
26 ms |
35704 KB |
Output is correct |
6 |
Correct |
25 ms |
35576 KB |
Output is correct |
7 |
Correct |
25 ms |
35576 KB |
Output is correct |
8 |
Correct |
25 ms |
35576 KB |
Output is correct |
9 |
Correct |
26 ms |
35576 KB |
Output is correct |
10 |
Correct |
25 ms |
35576 KB |
Output is correct |
11 |
Correct |
26 ms |
35576 KB |
Output is correct |
12 |
Correct |
25 ms |
35576 KB |
Output is correct |
13 |
Correct |
25 ms |
35576 KB |
Output is correct |
14 |
Correct |
25 ms |
35576 KB |
Output is correct |
15 |
Correct |
26 ms |
35576 KB |
Output is correct |
16 |
Correct |
26 ms |
35576 KB |
Output is correct |
17 |
Correct |
26 ms |
35576 KB |
Output is correct |
18 |
Correct |
26 ms |
35576 KB |
Output is correct |
19 |
Correct |
26 ms |
35576 KB |
Output is correct |
20 |
Correct |
26 ms |
35576 KB |
Output is correct |
21 |
Correct |
27 ms |
35576 KB |
Output is correct |
22 |
Correct |
25 ms |
35576 KB |
Output is correct |
23 |
Correct |
26 ms |
35576 KB |
Output is correct |
24 |
Correct |
27 ms |
35576 KB |
Output is correct |
25 |
Correct |
26 ms |
35576 KB |
Output is correct |
26 |
Correct |
28 ms |
36216 KB |
Output is correct |
27 |
Correct |
28 ms |
36088 KB |
Output is correct |
28 |
Correct |
28 ms |
36216 KB |
Output is correct |
29 |
Correct |
29 ms |
36216 KB |
Output is correct |
30 |
Correct |
27 ms |
36088 KB |
Output is correct |
31 |
Correct |
25 ms |
35576 KB |
Output is correct |
32 |
Correct |
27 ms |
36216 KB |
Output is correct |
33 |
Correct |
26 ms |
35576 KB |
Output is correct |
34 |
Correct |
27 ms |
36088 KB |
Output is correct |
35 |
Correct |
29 ms |
36216 KB |
Output is correct |
36 |
Correct |
28 ms |
36088 KB |
Output is correct |
37 |
Correct |
28 ms |
36088 KB |
Output is correct |
38 |
Correct |
27 ms |
35576 KB |
Output is correct |
39 |
Correct |
28 ms |
36088 KB |
Output is correct |
40 |
Correct |
34 ms |
36088 KB |
Output is correct |
41 |
Correct |
27 ms |
36088 KB |
Output is correct |
42 |
Correct |
27 ms |
36088 KB |
Output is correct |
43 |
Correct |
29 ms |
36164 KB |
Output is correct |
44 |
Correct |
25 ms |
35708 KB |
Output is correct |
45 |
Correct |
28 ms |
36088 KB |
Output is correct |
46 |
Correct |
27 ms |
36088 KB |
Output is correct |
47 |
Correct |
27 ms |
35576 KB |
Output is correct |
48 |
Correct |
28 ms |
36088 KB |
Output is correct |
49 |
Correct |
27 ms |
36216 KB |
Output is correct |
50 |
Correct |
29 ms |
36216 KB |
Output is correct |
51 |
Correct |
28 ms |
36088 KB |
Output is correct |
52 |
Correct |
27 ms |
36088 KB |
Output is correct |
53 |
Correct |
27 ms |
36088 KB |
Output is correct |
54 |
Correct |
28 ms |
36088 KB |
Output is correct |
55 |
Correct |
25 ms |
35576 KB |
Output is correct |
56 |
Correct |
116 ms |
50900 KB |
Output is correct |
57 |
Correct |
145 ms |
50552 KB |
Output is correct |
58 |
Correct |
27 ms |
36088 KB |
Output is correct |
59 |
Correct |
25 ms |
35576 KB |
Output is correct |
60 |
Correct |
26 ms |
35576 KB |
Output is correct |
61 |
Correct |
139 ms |
50424 KB |
Output is correct |
62 |
Correct |
27 ms |
36088 KB |
Output is correct |
63 |
Correct |
176 ms |
54744 KB |
Output is correct |
64 |
Correct |
146 ms |
50616 KB |
Output is correct |
65 |
Correct |
28 ms |
36088 KB |
Output is correct |
66 |
Correct |
157 ms |
50808 KB |
Output is correct |
67 |
Correct |
27 ms |
36088 KB |
Output is correct |
68 |
Correct |
27 ms |
36088 KB |
Output is correct |
69 |
Correct |
112 ms |
50800 KB |
Output is correct |
70 |
Correct |
27 ms |
36216 KB |
Output is correct |
71 |
Correct |
139 ms |
50540 KB |
Output is correct |
72 |
Correct |
171 ms |
52136 KB |
Output is correct |
73 |
Correct |
26 ms |
35576 KB |
Output is correct |
74 |
Correct |
166 ms |
54896 KB |
Output is correct |
75 |
Correct |
184 ms |
52728 KB |
Output is correct |
76 |
Correct |
28 ms |
36088 KB |
Output is correct |
77 |
Correct |
28 ms |
36088 KB |
Output is correct |
78 |
Correct |
112 ms |
49396 KB |
Output is correct |
79 |
Correct |
124 ms |
55532 KB |
Output is correct |
80 |
Correct |
34 ms |
36376 KB |
Output is correct |
81 |
Correct |
27 ms |
36088 KB |
Output is correct |
82 |
Correct |
25 ms |
35576 KB |
Output is correct |
83 |
Correct |
26 ms |
35580 KB |
Output is correct |
84 |
Correct |
27 ms |
36088 KB |
Output is correct |
85 |
Correct |
163 ms |
52344 KB |
Output is correct |
86 |
Correct |
27 ms |
36088 KB |
Output is correct |
87 |
Correct |
137 ms |
50680 KB |
Output is correct |
88 |
Correct |
25 ms |
35576 KB |
Output is correct |
89 |
Correct |
158 ms |
50680 KB |
Output is correct |
90 |
Correct |
164 ms |
52472 KB |
Output is correct |
91 |
Correct |
145 ms |
56696 KB |
Output is correct |
92 |
Correct |
114 ms |
50800 KB |
Output is correct |
93 |
Correct |
29 ms |
36092 KB |
Output is correct |
94 |
Correct |
26 ms |
35580 KB |
Output is correct |
95 |
Correct |
128 ms |
56300 KB |
Output is correct |
96 |
Correct |
168 ms |
59012 KB |
Output is correct |
97 |
Correct |
27 ms |
36092 KB |
Output is correct |
98 |
Correct |
25 ms |
35576 KB |
Output is correct |
99 |
Correct |
127 ms |
52720 KB |
Output is correct |
100 |
Correct |
27 ms |
36088 KB |
Output is correct |
101 |
Correct |
169 ms |
51320 KB |
Output is correct |
102 |
Correct |
169 ms |
58104 KB |
Output is correct |
103 |
Correct |
28 ms |
36216 KB |
Output is correct |
104 |
Correct |
27 ms |
36216 KB |
Output is correct |
105 |
Correct |
28 ms |
36088 KB |
Output is correct |
106 |
Correct |
27 ms |
36088 KB |
Output is correct |
107 |
Correct |
27 ms |
36088 KB |
Output is correct |
108 |
Correct |
836 ms |
120092 KB |
Output is correct |
109 |
Correct |
1084 ms |
121008 KB |
Output is correct |
110 |
Correct |
1104 ms |
127716 KB |
Output is correct |
111 |
Correct |
1132 ms |
154296 KB |
Output is correct |
112 |
Correct |
1123 ms |
143388 KB |
Output is correct |
113 |
Correct |
746 ms |
140432 KB |
Output is correct |
114 |
Correct |
829 ms |
110480 KB |
Output is correct |
115 |
Correct |
829 ms |
110404 KB |
Output is correct |
116 |
Correct |
1098 ms |
115064 KB |
Output is correct |
117 |
Correct |
951 ms |
142420 KB |
Output is correct |
118 |
Correct |
889 ms |
110968 KB |
Output is correct |
119 |
Correct |
964 ms |
142588 KB |
Output is correct |
120 |
Correct |
1110 ms |
150144 KB |
Output is correct |
121 |
Correct |
955 ms |
142460 KB |
Output is correct |
122 |
Correct |
1106 ms |
122976 KB |
Output is correct |
123 |
Correct |
737 ms |
145372 KB |
Output is correct |
124 |
Correct |
1078 ms |
134328 KB |
Output is correct |