#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 1;
int dsu[N], s[N], first[N], dep[N], par[N], last[N];
vector<int> adj[N], adj_[N], cities[N], euler, depth;
pair<int, int> e[N];
int sparse[N * 2][22];
int n, k, c = 0;
// DSU begins
int find(int v) {
if (dsu[v] == v)
return v;
else
return dsu[v] = find(dsu[v]);
}
void un(int u, int v) {
u = find(u), v = find(v);
if (u == v)
return;
else if (dep[u] < dep[v])
dsu[v] = u;
else
assert(dep[u] != dep[v]), dsu[u] = v;
}
// DSU ends
// LCA begins
void pre(int v, int p = -1, int d = 0) {
first[v] = c++;
dep[v] = d;
par[v] = p;
euler.push_back(v);
depth.push_back(d);
for (int i : adj[v]) {
if (i != p) {
pre(i, v, d + 1);
euler.push_back(v);
depth.push_back(d);
c++;
}
}
last[v] = c - 1;
}
int merge(int a, int b) {
return depth[a] < depth[b] ? a : b;
}
void build_sparse() {
for (int i = 0; i < euler.size(); i++)
sparse[i][0] = i;
for (int i = 1; i <= log2(euler.size()); i++) {
for (int j = 0; j < euler.size() && j - 1 + (1 << i) < euler.size(); j++)
sparse[j][i] = merge(sparse[j][i - 1], sparse[j + (1 << (i - 1))][i - 1]);
}
}
int lca(int u, int v) {
if (u == v) return u;
int f1 = min(first[u], first[v]), f2 = max(first[v], first[u]), diff = f2 - f1;
int dx = log2(diff);
return euler[merge(sparse[f1][dx], sparse[f2 - (1 << dx)][dx])];
}
// LCA ends
void unite(int low, int high) {
low = find(low), high = find(high);
while (dep[low] > dep[high])
un(low, high), low = find(par[low]);
}
void solveTestCase() {
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
e[i] = {u, v};
adj[u].push_back(v);
adj[v].push_back(u);
}
euler.reserve(N * 2);
depth.reserve(N * 2);
iota(dsu, dsu + N, 0);
pre(0);
build_sparse();
for (int i = 0; i < n; i++) {
cin >> s[i];
cities[s[i]].push_back(i);
}
for (int i = 0; i <= k; i++) {
if (cities[i].size() <= 1)
continue;
int high = lca(cities[i][0], cities[i][1]);
for (int j = 1; j < cities[i].size(); j++)
high = lca(high, cities[i][j]);
for (int j : cities[i])
unite(j, high);
}
for (int i = 0; i < n - 1; i++) {
if (find(e[i].first) != find(e[i].second))
adj_[find(e[i].first)].push_back(find(e[i].second)), adj_[find(e[i].second)].push_back(find(e[i].first));
}
int leaves = 0;
for (int i = 0; i < n; i++) {
if (adj_[i].size() == 1)
leaves++;
}
cout << (leaves + 1) / 2;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
for (int i = 1; i <= test; i++)
solveTestCase();
}
Compilation message
mergers.cpp: In function 'void build_sparse()':
mergers.cpp:57:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < euler.size(); i++)
~~^~~~~~~~~~~~~~
mergers.cpp:61:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < euler.size() && j - 1 + (1 << i) < euler.size(); j++)
~~^~~~~~~~~~~~~~
mergers.cpp:61:62: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < euler.size() && j - 1 + (1 << i) < euler.size(); j++)
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
mergers.cpp: In function 'void solveTestCase()':
mergers.cpp:108:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j < cities[i].size(); j++)
~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
39552 KB |
Output is correct |
2 |
Correct |
26 ms |
39552 KB |
Output is correct |
3 |
Correct |
26 ms |
39672 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
26 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39540 KB |
Output is correct |
7 |
Correct |
26 ms |
39552 KB |
Output is correct |
8 |
Correct |
25 ms |
39552 KB |
Output is correct |
9 |
Correct |
25 ms |
39552 KB |
Output is correct |
10 |
Correct |
27 ms |
39680 KB |
Output is correct |
11 |
Correct |
26 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39552 KB |
Output is correct |
13 |
Correct |
26 ms |
39552 KB |
Output is correct |
14 |
Correct |
27 ms |
39528 KB |
Output is correct |
15 |
Correct |
26 ms |
39552 KB |
Output is correct |
16 |
Correct |
27 ms |
39548 KB |
Output is correct |
17 |
Correct |
28 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
27 ms |
39552 KB |
Output is correct |
20 |
Correct |
26 ms |
39552 KB |
Output is correct |
21 |
Correct |
27 ms |
39552 KB |
Output is correct |
22 |
Correct |
27 ms |
39552 KB |
Output is correct |
23 |
Correct |
26 ms |
39552 KB |
Output is correct |
24 |
Correct |
26 ms |
39552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
39552 KB |
Output is correct |
2 |
Correct |
26 ms |
39552 KB |
Output is correct |
3 |
Correct |
26 ms |
39672 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
26 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39540 KB |
Output is correct |
7 |
Correct |
26 ms |
39552 KB |
Output is correct |
8 |
Correct |
25 ms |
39552 KB |
Output is correct |
9 |
Correct |
25 ms |
39552 KB |
Output is correct |
10 |
Correct |
27 ms |
39680 KB |
Output is correct |
11 |
Correct |
26 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39552 KB |
Output is correct |
13 |
Correct |
26 ms |
39552 KB |
Output is correct |
14 |
Correct |
27 ms |
39528 KB |
Output is correct |
15 |
Correct |
26 ms |
39552 KB |
Output is correct |
16 |
Correct |
27 ms |
39548 KB |
Output is correct |
17 |
Correct |
28 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
27 ms |
39552 KB |
Output is correct |
20 |
Correct |
26 ms |
39552 KB |
Output is correct |
21 |
Correct |
27 ms |
39552 KB |
Output is correct |
22 |
Correct |
27 ms |
39552 KB |
Output is correct |
23 |
Correct |
26 ms |
39552 KB |
Output is correct |
24 |
Correct |
26 ms |
39552 KB |
Output is correct |
25 |
Correct |
27 ms |
39552 KB |
Output is correct |
26 |
Correct |
28 ms |
41088 KB |
Output is correct |
27 |
Correct |
29 ms |
40960 KB |
Output is correct |
28 |
Correct |
29 ms |
41216 KB |
Output is correct |
29 |
Correct |
30 ms |
41216 KB |
Output is correct |
30 |
Correct |
29 ms |
40960 KB |
Output is correct |
31 |
Correct |
26 ms |
39552 KB |
Output is correct |
32 |
Correct |
29 ms |
41216 KB |
Output is correct |
33 |
Correct |
26 ms |
39552 KB |
Output is correct |
34 |
Correct |
29 ms |
41216 KB |
Output is correct |
35 |
Correct |
30 ms |
41088 KB |
Output is correct |
36 |
Correct |
32 ms |
40960 KB |
Output is correct |
37 |
Correct |
29 ms |
41088 KB |
Output is correct |
38 |
Correct |
26 ms |
39552 KB |
Output is correct |
39 |
Correct |
29 ms |
40960 KB |
Output is correct |
40 |
Correct |
28 ms |
40960 KB |
Output is correct |
41 |
Correct |
29 ms |
40960 KB |
Output is correct |
42 |
Correct |
29 ms |
41088 KB |
Output is correct |
43 |
Correct |
29 ms |
41088 KB |
Output is correct |
44 |
Correct |
27 ms |
39680 KB |
Output is correct |
45 |
Correct |
29 ms |
41088 KB |
Output is correct |
46 |
Correct |
29 ms |
40960 KB |
Output is correct |
47 |
Correct |
28 ms |
39552 KB |
Output is correct |
48 |
Correct |
29 ms |
40960 KB |
Output is correct |
49 |
Correct |
30 ms |
41216 KB |
Output is correct |
50 |
Correct |
29 ms |
41216 KB |
Output is correct |
51 |
Correct |
29 ms |
40960 KB |
Output is correct |
52 |
Correct |
30 ms |
41080 KB |
Output is correct |
53 |
Correct |
28 ms |
41088 KB |
Output is correct |
54 |
Correct |
29 ms |
40960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
39552 KB |
Output is correct |
2 |
Correct |
26 ms |
39552 KB |
Output is correct |
3 |
Correct |
26 ms |
39672 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
26 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39540 KB |
Output is correct |
7 |
Correct |
26 ms |
39552 KB |
Output is correct |
8 |
Correct |
25 ms |
39552 KB |
Output is correct |
9 |
Correct |
25 ms |
39552 KB |
Output is correct |
10 |
Correct |
27 ms |
39680 KB |
Output is correct |
11 |
Correct |
26 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39552 KB |
Output is correct |
13 |
Correct |
26 ms |
39552 KB |
Output is correct |
14 |
Correct |
27 ms |
39528 KB |
Output is correct |
15 |
Correct |
26 ms |
39552 KB |
Output is correct |
16 |
Correct |
27 ms |
39548 KB |
Output is correct |
17 |
Correct |
28 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
27 ms |
39552 KB |
Output is correct |
20 |
Correct |
26 ms |
39552 KB |
Output is correct |
21 |
Correct |
27 ms |
39552 KB |
Output is correct |
22 |
Correct |
27 ms |
39552 KB |
Output is correct |
23 |
Correct |
26 ms |
39552 KB |
Output is correct |
24 |
Correct |
26 ms |
39552 KB |
Output is correct |
25 |
Correct |
27 ms |
39552 KB |
Output is correct |
26 |
Correct |
166 ms |
87532 KB |
Output is correct |
27 |
Correct |
175 ms |
87264 KB |
Output is correct |
28 |
Correct |
28 ms |
40960 KB |
Output is correct |
29 |
Correct |
26 ms |
39552 KB |
Output is correct |
30 |
Correct |
26 ms |
39552 KB |
Output is correct |
31 |
Correct |
163 ms |
87288 KB |
Output is correct |
32 |
Correct |
30 ms |
40960 KB |
Output is correct |
33 |
Correct |
164 ms |
91004 KB |
Output is correct |
34 |
Correct |
169 ms |
87416 KB |
Output is correct |
35 |
Correct |
29 ms |
40960 KB |
Output is correct |
36 |
Correct |
163 ms |
87136 KB |
Output is correct |
37 |
Correct |
29 ms |
41088 KB |
Output is correct |
38 |
Correct |
29 ms |
40960 KB |
Output is correct |
39 |
Correct |
152 ms |
87656 KB |
Output is correct |
40 |
Correct |
29 ms |
41088 KB |
Output is correct |
41 |
Correct |
162 ms |
87404 KB |
Output is correct |
42 |
Correct |
166 ms |
88184 KB |
Output is correct |
43 |
Correct |
26 ms |
39552 KB |
Output is correct |
44 |
Correct |
168 ms |
91120 KB |
Output is correct |
45 |
Correct |
200 ms |
90232 KB |
Output is correct |
46 |
Correct |
29 ms |
41088 KB |
Output is correct |
47 |
Correct |
29 ms |
40960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
87692 KB |
Output is correct |
2 |
Correct |
152 ms |
93524 KB |
Output is correct |
3 |
Correct |
30 ms |
41088 KB |
Output is correct |
4 |
Correct |
29 ms |
40960 KB |
Output is correct |
5 |
Correct |
27 ms |
39552 KB |
Output is correct |
6 |
Correct |
27 ms |
39552 KB |
Output is correct |
7 |
Correct |
28 ms |
40960 KB |
Output is correct |
8 |
Correct |
192 ms |
88696 KB |
Output is correct |
9 |
Correct |
30 ms |
40960 KB |
Output is correct |
10 |
Correct |
162 ms |
87468 KB |
Output is correct |
11 |
Correct |
27 ms |
39544 KB |
Output is correct |
12 |
Correct |
162 ms |
86928 KB |
Output is correct |
13 |
Correct |
191 ms |
88824 KB |
Output is correct |
14 |
Correct |
179 ms |
93176 KB |
Output is correct |
15 |
Correct |
156 ms |
87652 KB |
Output is correct |
16 |
Correct |
29 ms |
41088 KB |
Output is correct |
17 |
Correct |
26 ms |
39552 KB |
Output is correct |
18 |
Correct |
161 ms |
92512 KB |
Output is correct |
19 |
Correct |
185 ms |
94456 KB |
Output is correct |
20 |
Correct |
29 ms |
40960 KB |
Output is correct |
21 |
Correct |
26 ms |
39680 KB |
Output is correct |
22 |
Correct |
176 ms |
89196 KB |
Output is correct |
23 |
Correct |
29 ms |
41088 KB |
Output is correct |
24 |
Correct |
178 ms |
87800 KB |
Output is correct |
25 |
Correct |
189 ms |
95736 KB |
Output is correct |
26 |
Correct |
29 ms |
41216 KB |
Output is correct |
27 |
Correct |
29 ms |
41216 KB |
Output is correct |
28 |
Correct |
29 ms |
41088 KB |
Output is correct |
29 |
Correct |
28 ms |
40960 KB |
Output is correct |
30 |
Correct |
29 ms |
41088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
39552 KB |
Output is correct |
2 |
Correct |
26 ms |
39552 KB |
Output is correct |
3 |
Correct |
26 ms |
39672 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
26 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39540 KB |
Output is correct |
7 |
Correct |
26 ms |
39552 KB |
Output is correct |
8 |
Correct |
25 ms |
39552 KB |
Output is correct |
9 |
Correct |
25 ms |
39552 KB |
Output is correct |
10 |
Correct |
27 ms |
39680 KB |
Output is correct |
11 |
Correct |
26 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39552 KB |
Output is correct |
13 |
Correct |
26 ms |
39552 KB |
Output is correct |
14 |
Correct |
27 ms |
39528 KB |
Output is correct |
15 |
Correct |
26 ms |
39552 KB |
Output is correct |
16 |
Correct |
27 ms |
39548 KB |
Output is correct |
17 |
Correct |
28 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
27 ms |
39552 KB |
Output is correct |
20 |
Correct |
26 ms |
39552 KB |
Output is correct |
21 |
Correct |
27 ms |
39552 KB |
Output is correct |
22 |
Correct |
27 ms |
39552 KB |
Output is correct |
23 |
Correct |
26 ms |
39552 KB |
Output is correct |
24 |
Correct |
26 ms |
39552 KB |
Output is correct |
25 |
Correct |
27 ms |
39552 KB |
Output is correct |
26 |
Correct |
28 ms |
41088 KB |
Output is correct |
27 |
Correct |
29 ms |
40960 KB |
Output is correct |
28 |
Correct |
29 ms |
41216 KB |
Output is correct |
29 |
Correct |
30 ms |
41216 KB |
Output is correct |
30 |
Correct |
29 ms |
40960 KB |
Output is correct |
31 |
Correct |
26 ms |
39552 KB |
Output is correct |
32 |
Correct |
29 ms |
41216 KB |
Output is correct |
33 |
Correct |
26 ms |
39552 KB |
Output is correct |
34 |
Correct |
29 ms |
41216 KB |
Output is correct |
35 |
Correct |
30 ms |
41088 KB |
Output is correct |
36 |
Correct |
32 ms |
40960 KB |
Output is correct |
37 |
Correct |
29 ms |
41088 KB |
Output is correct |
38 |
Correct |
26 ms |
39552 KB |
Output is correct |
39 |
Correct |
29 ms |
40960 KB |
Output is correct |
40 |
Correct |
28 ms |
40960 KB |
Output is correct |
41 |
Correct |
29 ms |
40960 KB |
Output is correct |
42 |
Correct |
29 ms |
41088 KB |
Output is correct |
43 |
Correct |
29 ms |
41088 KB |
Output is correct |
44 |
Correct |
27 ms |
39680 KB |
Output is correct |
45 |
Correct |
29 ms |
41088 KB |
Output is correct |
46 |
Correct |
29 ms |
40960 KB |
Output is correct |
47 |
Correct |
28 ms |
39552 KB |
Output is correct |
48 |
Correct |
29 ms |
40960 KB |
Output is correct |
49 |
Correct |
30 ms |
41216 KB |
Output is correct |
50 |
Correct |
29 ms |
41216 KB |
Output is correct |
51 |
Correct |
29 ms |
40960 KB |
Output is correct |
52 |
Correct |
30 ms |
41080 KB |
Output is correct |
53 |
Correct |
28 ms |
41088 KB |
Output is correct |
54 |
Correct |
29 ms |
40960 KB |
Output is correct |
55 |
Correct |
27 ms |
39552 KB |
Output is correct |
56 |
Correct |
166 ms |
87532 KB |
Output is correct |
57 |
Correct |
175 ms |
87264 KB |
Output is correct |
58 |
Correct |
28 ms |
40960 KB |
Output is correct |
59 |
Correct |
26 ms |
39552 KB |
Output is correct |
60 |
Correct |
26 ms |
39552 KB |
Output is correct |
61 |
Correct |
163 ms |
87288 KB |
Output is correct |
62 |
Correct |
30 ms |
40960 KB |
Output is correct |
63 |
Correct |
164 ms |
91004 KB |
Output is correct |
64 |
Correct |
169 ms |
87416 KB |
Output is correct |
65 |
Correct |
29 ms |
40960 KB |
Output is correct |
66 |
Correct |
163 ms |
87136 KB |
Output is correct |
67 |
Correct |
29 ms |
41088 KB |
Output is correct |
68 |
Correct |
29 ms |
40960 KB |
Output is correct |
69 |
Correct |
152 ms |
87656 KB |
Output is correct |
70 |
Correct |
29 ms |
41088 KB |
Output is correct |
71 |
Correct |
162 ms |
87404 KB |
Output is correct |
72 |
Correct |
166 ms |
88184 KB |
Output is correct |
73 |
Correct |
26 ms |
39552 KB |
Output is correct |
74 |
Correct |
168 ms |
91120 KB |
Output is correct |
75 |
Correct |
200 ms |
90232 KB |
Output is correct |
76 |
Correct |
29 ms |
41088 KB |
Output is correct |
77 |
Correct |
29 ms |
40960 KB |
Output is correct |
78 |
Correct |
158 ms |
87692 KB |
Output is correct |
79 |
Correct |
152 ms |
93524 KB |
Output is correct |
80 |
Correct |
30 ms |
41088 KB |
Output is correct |
81 |
Correct |
29 ms |
40960 KB |
Output is correct |
82 |
Correct |
27 ms |
39552 KB |
Output is correct |
83 |
Correct |
27 ms |
39552 KB |
Output is correct |
84 |
Correct |
28 ms |
40960 KB |
Output is correct |
85 |
Correct |
192 ms |
88696 KB |
Output is correct |
86 |
Correct |
30 ms |
40960 KB |
Output is correct |
87 |
Correct |
162 ms |
87468 KB |
Output is correct |
88 |
Correct |
27 ms |
39544 KB |
Output is correct |
89 |
Correct |
162 ms |
86928 KB |
Output is correct |
90 |
Correct |
191 ms |
88824 KB |
Output is correct |
91 |
Correct |
179 ms |
93176 KB |
Output is correct |
92 |
Correct |
156 ms |
87652 KB |
Output is correct |
93 |
Correct |
29 ms |
41088 KB |
Output is correct |
94 |
Correct |
26 ms |
39552 KB |
Output is correct |
95 |
Correct |
161 ms |
92512 KB |
Output is correct |
96 |
Correct |
185 ms |
94456 KB |
Output is correct |
97 |
Correct |
29 ms |
40960 KB |
Output is correct |
98 |
Correct |
26 ms |
39680 KB |
Output is correct |
99 |
Correct |
176 ms |
89196 KB |
Output is correct |
100 |
Correct |
29 ms |
41088 KB |
Output is correct |
101 |
Correct |
178 ms |
87800 KB |
Output is correct |
102 |
Correct |
189 ms |
95736 KB |
Output is correct |
103 |
Correct |
29 ms |
41216 KB |
Output is correct |
104 |
Correct |
29 ms |
41216 KB |
Output is correct |
105 |
Correct |
29 ms |
41088 KB |
Output is correct |
106 |
Correct |
28 ms |
40960 KB |
Output is correct |
107 |
Correct |
29 ms |
41088 KB |
Output is correct |
108 |
Runtime error |
343 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
109 |
Halted |
0 ms |
0 KB |
- |