#include <iostream>
#include <vector>
#include <array>
#define ll long long
using namespace std;
vector <ll> adj[500000];
vector <ll> G[500000];
vector <array<ll, 2>> edge;
ll n, k, a, b, p, cnt, f, A[500000], dp[500000], D[500000], H[500000], P[500000], bl[500000][19], deg[500000];
ll dsu(ll u) {
if (P[u] == u) return u;
else return P[u] = dsu(P[u]);
}
void dfs(ll u, ll p) {
for (auto v : adj[u]) {
if (v != p) {
D[v] = D[u]+1;
dfs(v, u);
bl[v][0] = u;
}
}
}
ll lca(ll a, ll b) {
if (D[a] > D[b]) swap(a, b);
ll db = D[b];
for (int i=18; i>=0; --i) {
if (db-(1LL<<i) >= D[a]) {
db -= (1LL<<i);
b = bl[b][i];
}
}
for (int i=18; i>=0; --i) {
if (bl[a][i] != bl[b][i]) {
a = bl[a][i], b = bl[b][i];
}
}
if (a == b) return a;
else return bl[a][0];
}
void solve(ll u, ll p) {
dp[u] = H[A[u]];
for (auto v : adj[u]) {
if (v != p) {
solve(v, u);
if (dp[v] > D[u]) {
++cnt;
edge.push_back({u, v});
}
else P[v] = u;
dp[u] = min(dp[u], dp[v]);
}
}
}
int main() {
cin >> n >> k;
for (int i=0; i<n-1; ++i) {
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=0; i<n; ++i) {
P[i] = i;
cin >> A[i];
--A[i];
G[A[i]].push_back(i);
}
dfs(0, -1);
for (int j=1; j<19; ++j) {
for (int i=0; i<n; ++i) {
bl[i][j] = bl[bl[i][j-1]][j-1];
}
}
for (int i=0; i<k; ++i) {
p = G[i][0];
for (int j=1; j<G[i].size(); ++j) {
p = lca(p, G[i][j]);
}
H[i] = D[p];
}
solve(0, -1);
for (auto [u, v] : edge) {
auto a = dsu(u);
auto b = dsu(v);
++deg[a];
++deg[b];
}
for (int i=0; i<n; ++i) {
if (deg[i] == 1) ++f;
}
cout << (f+1)/2 << '\n';
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:83:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int j=1; j<G[i].size(); ++j) {
| ~^~~~~~~~~~~~
mergers.cpp:89:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
89 | for (auto [u, v] : edge) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35152 KB |
Output is correct |
2 |
Correct |
8 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35376 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
9 ms |
35356 KB |
Output is correct |
6 |
Correct |
10 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33368 KB |
Output is correct |
8 |
Correct |
8 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35392 KB |
Output is correct |
10 |
Correct |
7 ms |
35416 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35416 KB |
Output is correct |
13 |
Correct |
8 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
8 ms |
35420 KB |
Output is correct |
17 |
Correct |
8 ms |
35420 KB |
Output is correct |
18 |
Correct |
8 ms |
35264 KB |
Output is correct |
19 |
Correct |
8 ms |
35420 KB |
Output is correct |
20 |
Correct |
8 ms |
35420 KB |
Output is correct |
21 |
Correct |
8 ms |
35420 KB |
Output is correct |
22 |
Correct |
8 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35420 KB |
Output is correct |
24 |
Correct |
7 ms |
35424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35152 KB |
Output is correct |
2 |
Correct |
8 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35376 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
9 ms |
35356 KB |
Output is correct |
6 |
Correct |
10 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33368 KB |
Output is correct |
8 |
Correct |
8 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35392 KB |
Output is correct |
10 |
Correct |
7 ms |
35416 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35416 KB |
Output is correct |
13 |
Correct |
8 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
8 ms |
35420 KB |
Output is correct |
17 |
Correct |
8 ms |
35420 KB |
Output is correct |
18 |
Correct |
8 ms |
35264 KB |
Output is correct |
19 |
Correct |
8 ms |
35420 KB |
Output is correct |
20 |
Correct |
8 ms |
35420 KB |
Output is correct |
21 |
Correct |
8 ms |
35420 KB |
Output is correct |
22 |
Correct |
8 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35420 KB |
Output is correct |
24 |
Correct |
7 ms |
35424 KB |
Output is correct |
25 |
Correct |
8 ms |
35416 KB |
Output is correct |
26 |
Correct |
11 ms |
37724 KB |
Output is correct |
27 |
Correct |
12 ms |
37720 KB |
Output is correct |
28 |
Correct |
10 ms |
37980 KB |
Output is correct |
29 |
Correct |
10 ms |
37724 KB |
Output is correct |
30 |
Correct |
10 ms |
37464 KB |
Output is correct |
31 |
Correct |
7 ms |
35416 KB |
Output is correct |
32 |
Correct |
10 ms |
37980 KB |
Output is correct |
33 |
Correct |
7 ms |
35420 KB |
Output is correct |
34 |
Correct |
10 ms |
37468 KB |
Output is correct |
35 |
Correct |
10 ms |
37720 KB |
Output is correct |
36 |
Correct |
10 ms |
37468 KB |
Output is correct |
37 |
Correct |
10 ms |
37640 KB |
Output is correct |
38 |
Correct |
8 ms |
35388 KB |
Output is correct |
39 |
Correct |
10 ms |
37724 KB |
Output is correct |
40 |
Correct |
10 ms |
37468 KB |
Output is correct |
41 |
Correct |
10 ms |
37724 KB |
Output is correct |
42 |
Correct |
10 ms |
37568 KB |
Output is correct |
43 |
Correct |
10 ms |
37980 KB |
Output is correct |
44 |
Correct |
7 ms |
35416 KB |
Output is correct |
45 |
Correct |
10 ms |
37720 KB |
Output is correct |
46 |
Correct |
10 ms |
37664 KB |
Output is correct |
47 |
Correct |
7 ms |
35672 KB |
Output is correct |
48 |
Correct |
11 ms |
37724 KB |
Output is correct |
49 |
Correct |
11 ms |
37856 KB |
Output is correct |
50 |
Correct |
10 ms |
37724 KB |
Output is correct |
51 |
Correct |
10 ms |
37724 KB |
Output is correct |
52 |
Correct |
10 ms |
37468 KB |
Output is correct |
53 |
Correct |
10 ms |
37976 KB |
Output is correct |
54 |
Correct |
12 ms |
37468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35152 KB |
Output is correct |
2 |
Correct |
8 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35376 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
9 ms |
35356 KB |
Output is correct |
6 |
Correct |
10 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33368 KB |
Output is correct |
8 |
Correct |
8 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35392 KB |
Output is correct |
10 |
Correct |
7 ms |
35416 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35416 KB |
Output is correct |
13 |
Correct |
8 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
8 ms |
35420 KB |
Output is correct |
17 |
Correct |
8 ms |
35420 KB |
Output is correct |
18 |
Correct |
8 ms |
35264 KB |
Output is correct |
19 |
Correct |
8 ms |
35420 KB |
Output is correct |
20 |
Correct |
8 ms |
35420 KB |
Output is correct |
21 |
Correct |
8 ms |
35420 KB |
Output is correct |
22 |
Correct |
8 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35420 KB |
Output is correct |
24 |
Correct |
7 ms |
35424 KB |
Output is correct |
25 |
Correct |
7 ms |
35420 KB |
Output is correct |
26 |
Correct |
117 ms |
64456 KB |
Output is correct |
27 |
Correct |
116 ms |
64340 KB |
Output is correct |
28 |
Correct |
10 ms |
37464 KB |
Output is correct |
29 |
Correct |
7 ms |
35420 KB |
Output is correct |
30 |
Correct |
7 ms |
35412 KB |
Output is correct |
31 |
Correct |
117 ms |
66420 KB |
Output is correct |
32 |
Correct |
10 ms |
37452 KB |
Output is correct |
33 |
Correct |
143 ms |
73812 KB |
Output is correct |
34 |
Correct |
126 ms |
66364 KB |
Output is correct |
35 |
Correct |
10 ms |
37464 KB |
Output is correct |
36 |
Correct |
127 ms |
66968 KB |
Output is correct |
37 |
Correct |
10 ms |
37468 KB |
Output is correct |
38 |
Correct |
10 ms |
37688 KB |
Output is correct |
39 |
Correct |
102 ms |
64580 KB |
Output is correct |
40 |
Correct |
10 ms |
37980 KB |
Output is correct |
41 |
Correct |
108 ms |
63992 KB |
Output is correct |
42 |
Correct |
133 ms |
69288 KB |
Output is correct |
43 |
Correct |
7 ms |
35416 KB |
Output is correct |
44 |
Correct |
146 ms |
74020 KB |
Output is correct |
45 |
Correct |
131 ms |
70176 KB |
Output is correct |
46 |
Correct |
10 ms |
37468 KB |
Output is correct |
47 |
Correct |
10 ms |
37668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
64452 KB |
Output is correct |
2 |
Correct |
103 ms |
72764 KB |
Output is correct |
3 |
Correct |
10 ms |
37720 KB |
Output is correct |
4 |
Correct |
10 ms |
37720 KB |
Output is correct |
5 |
Correct |
7 ms |
35428 KB |
Output is correct |
6 |
Correct |
7 ms |
35672 KB |
Output is correct |
7 |
Correct |
10 ms |
37468 KB |
Output is correct |
8 |
Correct |
131 ms |
70064 KB |
Output is correct |
9 |
Correct |
10 ms |
37464 KB |
Output is correct |
10 |
Correct |
125 ms |
66912 KB |
Output is correct |
11 |
Correct |
8 ms |
35416 KB |
Output is correct |
12 |
Correct |
125 ms |
66228 KB |
Output is correct |
13 |
Correct |
141 ms |
70028 KB |
Output is correct |
14 |
Correct |
120 ms |
72656 KB |
Output is correct |
15 |
Correct |
107 ms |
64576 KB |
Output is correct |
16 |
Correct |
10 ms |
37544 KB |
Output is correct |
17 |
Correct |
9 ms |
35432 KB |
Output is correct |
18 |
Correct |
113 ms |
72332 KB |
Output is correct |
19 |
Correct |
152 ms |
77764 KB |
Output is correct |
20 |
Correct |
10 ms |
37724 KB |
Output is correct |
21 |
Correct |
7 ms |
35420 KB |
Output is correct |
22 |
Correct |
120 ms |
70464 KB |
Output is correct |
23 |
Correct |
10 ms |
37720 KB |
Output is correct |
24 |
Correct |
132 ms |
69324 KB |
Output is correct |
25 |
Correct |
132 ms |
76544 KB |
Output is correct |
26 |
Correct |
9 ms |
37724 KB |
Output is correct |
27 |
Correct |
10 ms |
37816 KB |
Output is correct |
28 |
Correct |
10 ms |
37724 KB |
Output is correct |
29 |
Correct |
10 ms |
37712 KB |
Output is correct |
30 |
Correct |
10 ms |
37720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35152 KB |
Output is correct |
2 |
Correct |
8 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35376 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
9 ms |
35356 KB |
Output is correct |
6 |
Correct |
10 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33368 KB |
Output is correct |
8 |
Correct |
8 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35392 KB |
Output is correct |
10 |
Correct |
7 ms |
35416 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35416 KB |
Output is correct |
13 |
Correct |
8 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
8 ms |
35420 KB |
Output is correct |
17 |
Correct |
8 ms |
35420 KB |
Output is correct |
18 |
Correct |
8 ms |
35264 KB |
Output is correct |
19 |
Correct |
8 ms |
35420 KB |
Output is correct |
20 |
Correct |
8 ms |
35420 KB |
Output is correct |
21 |
Correct |
8 ms |
35420 KB |
Output is correct |
22 |
Correct |
8 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35420 KB |
Output is correct |
24 |
Correct |
7 ms |
35424 KB |
Output is correct |
25 |
Correct |
8 ms |
35416 KB |
Output is correct |
26 |
Correct |
11 ms |
37724 KB |
Output is correct |
27 |
Correct |
12 ms |
37720 KB |
Output is correct |
28 |
Correct |
10 ms |
37980 KB |
Output is correct |
29 |
Correct |
10 ms |
37724 KB |
Output is correct |
30 |
Correct |
10 ms |
37464 KB |
Output is correct |
31 |
Correct |
7 ms |
35416 KB |
Output is correct |
32 |
Correct |
10 ms |
37980 KB |
Output is correct |
33 |
Correct |
7 ms |
35420 KB |
Output is correct |
34 |
Correct |
10 ms |
37468 KB |
Output is correct |
35 |
Correct |
10 ms |
37720 KB |
Output is correct |
36 |
Correct |
10 ms |
37468 KB |
Output is correct |
37 |
Correct |
10 ms |
37640 KB |
Output is correct |
38 |
Correct |
8 ms |
35388 KB |
Output is correct |
39 |
Correct |
10 ms |
37724 KB |
Output is correct |
40 |
Correct |
10 ms |
37468 KB |
Output is correct |
41 |
Correct |
10 ms |
37724 KB |
Output is correct |
42 |
Correct |
10 ms |
37568 KB |
Output is correct |
43 |
Correct |
10 ms |
37980 KB |
Output is correct |
44 |
Correct |
7 ms |
35416 KB |
Output is correct |
45 |
Correct |
10 ms |
37720 KB |
Output is correct |
46 |
Correct |
10 ms |
37664 KB |
Output is correct |
47 |
Correct |
7 ms |
35672 KB |
Output is correct |
48 |
Correct |
11 ms |
37724 KB |
Output is correct |
49 |
Correct |
11 ms |
37856 KB |
Output is correct |
50 |
Correct |
10 ms |
37724 KB |
Output is correct |
51 |
Correct |
10 ms |
37724 KB |
Output is correct |
52 |
Correct |
10 ms |
37468 KB |
Output is correct |
53 |
Correct |
10 ms |
37976 KB |
Output is correct |
54 |
Correct |
12 ms |
37468 KB |
Output is correct |
55 |
Correct |
7 ms |
35420 KB |
Output is correct |
56 |
Correct |
117 ms |
64456 KB |
Output is correct |
57 |
Correct |
116 ms |
64340 KB |
Output is correct |
58 |
Correct |
10 ms |
37464 KB |
Output is correct |
59 |
Correct |
7 ms |
35420 KB |
Output is correct |
60 |
Correct |
7 ms |
35412 KB |
Output is correct |
61 |
Correct |
117 ms |
66420 KB |
Output is correct |
62 |
Correct |
10 ms |
37452 KB |
Output is correct |
63 |
Correct |
143 ms |
73812 KB |
Output is correct |
64 |
Correct |
126 ms |
66364 KB |
Output is correct |
65 |
Correct |
10 ms |
37464 KB |
Output is correct |
66 |
Correct |
127 ms |
66968 KB |
Output is correct |
67 |
Correct |
10 ms |
37468 KB |
Output is correct |
68 |
Correct |
10 ms |
37688 KB |
Output is correct |
69 |
Correct |
102 ms |
64580 KB |
Output is correct |
70 |
Correct |
10 ms |
37980 KB |
Output is correct |
71 |
Correct |
108 ms |
63992 KB |
Output is correct |
72 |
Correct |
133 ms |
69288 KB |
Output is correct |
73 |
Correct |
7 ms |
35416 KB |
Output is correct |
74 |
Correct |
146 ms |
74020 KB |
Output is correct |
75 |
Correct |
131 ms |
70176 KB |
Output is correct |
76 |
Correct |
10 ms |
37468 KB |
Output is correct |
77 |
Correct |
10 ms |
37668 KB |
Output is correct |
78 |
Correct |
108 ms |
64452 KB |
Output is correct |
79 |
Correct |
103 ms |
72764 KB |
Output is correct |
80 |
Correct |
10 ms |
37720 KB |
Output is correct |
81 |
Correct |
10 ms |
37720 KB |
Output is correct |
82 |
Correct |
7 ms |
35428 KB |
Output is correct |
83 |
Correct |
7 ms |
35672 KB |
Output is correct |
84 |
Correct |
10 ms |
37468 KB |
Output is correct |
85 |
Correct |
131 ms |
70064 KB |
Output is correct |
86 |
Correct |
10 ms |
37464 KB |
Output is correct |
87 |
Correct |
125 ms |
66912 KB |
Output is correct |
88 |
Correct |
8 ms |
35416 KB |
Output is correct |
89 |
Correct |
125 ms |
66228 KB |
Output is correct |
90 |
Correct |
141 ms |
70028 KB |
Output is correct |
91 |
Correct |
120 ms |
72656 KB |
Output is correct |
92 |
Correct |
107 ms |
64576 KB |
Output is correct |
93 |
Correct |
10 ms |
37544 KB |
Output is correct |
94 |
Correct |
9 ms |
35432 KB |
Output is correct |
95 |
Correct |
113 ms |
72332 KB |
Output is correct |
96 |
Correct |
152 ms |
77764 KB |
Output is correct |
97 |
Correct |
10 ms |
37724 KB |
Output is correct |
98 |
Correct |
7 ms |
35420 KB |
Output is correct |
99 |
Correct |
120 ms |
70464 KB |
Output is correct |
100 |
Correct |
10 ms |
37720 KB |
Output is correct |
101 |
Correct |
132 ms |
69324 KB |
Output is correct |
102 |
Correct |
132 ms |
76544 KB |
Output is correct |
103 |
Correct |
9 ms |
37724 KB |
Output is correct |
104 |
Correct |
10 ms |
37816 KB |
Output is correct |
105 |
Correct |
10 ms |
37724 KB |
Output is correct |
106 |
Correct |
10 ms |
37712 KB |
Output is correct |
107 |
Correct |
10 ms |
37720 KB |
Output is correct |
108 |
Correct |
738 ms |
159656 KB |
Output is correct |
109 |
Correct |
1070 ms |
170956 KB |
Output is correct |
110 |
Correct |
1121 ms |
184476 KB |
Output is correct |
111 |
Correct |
1040 ms |
203364 KB |
Output is correct |
112 |
Correct |
1030 ms |
193972 KB |
Output is correct |
113 |
Correct |
675 ms |
172308 KB |
Output is correct |
114 |
Correct |
716 ms |
146068 KB |
Output is correct |
115 |
Correct |
728 ms |
146060 KB |
Output is correct |
116 |
Correct |
901 ms |
155712 KB |
Output is correct |
117 |
Correct |
827 ms |
173828 KB |
Output is correct |
118 |
Correct |
808 ms |
150752 KB |
Output is correct |
119 |
Correct |
849 ms |
174004 KB |
Output is correct |
120 |
Correct |
987 ms |
195472 KB |
Output is correct |
121 |
Correct |
815 ms |
174464 KB |
Output is correct |
122 |
Correct |
889 ms |
160352 KB |
Output is correct |
123 |
Correct |
626 ms |
174852 KB |
Output is correct |
124 |
Correct |
910 ms |
169596 KB |
Output is correct |