#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
const int mxN = 5e5 + 1;
vector<int> adj[mxN];
int a[mxN];
vector<int> state[mxN];
int n, k;
void Input() {
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
state[a[i]].pb(i);
}
}
int tt = 0;
int dep[mxN];
pii range[mxN];
int lift[mxN][20];
void dfs1(int u = 1, int par = 0, int depth = 1) {
dep[u] = depth;
range[u].ff = ++tt;
lift[u][0] = par;
for (int &v : adj[u]) {
if (v == par) continue;
dfs1(v, u, depth + 1);
}
range[u].ss = tt;
}
void BuildLift() {
for (int bit = 1; bit < 20; bit++) {
for (int i = 1; i <= n; i++) {
lift[i][bit] = lift[lift[i][bit - 1]][bit - 1];
}
}
}
int FindLCA(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
int diff = dep[v] - dep[u];
for (int i = 19; i >= 0; i--) {
if (diff & (1 << i)) {
v = lift[v][i];
}
}
if (u == v) return u;
for (int i = 19; i >= 0; i--) {
if (lift[u][i] != lift[v][i]) {
u = lift[u][i];
v = lift[v][i];
}
}
return lift[u][0];
}
bool cmp(int &x, int &y) {
return range[x].ff < range[y].ff;
}
int dp[mxN];
void dfs2(int u = 1, int par = 0) {
for (int &v : adj[u]) {
if (v == par) continue;
dfs2(v, u);
dp[u] += dp[v];
}
}
int dsupp[mxN];
int DSUFind(int u) {
if (dsupp[u] == 0) return u;
return (dsupp[u] = DSUFind(dsupp[u]));
}
void DSUMerge(int u, int v) {
u = DSUFind(u);
v = DSUFind(v);
if (u != v) dsupp[v] = u;
}
int deg[mxN];
void solve() {
Input();
dfs1();
BuildLift();
for (int i = 1; i <= k; i++) {
sort(state[i].begin(), state[i].end(), cmp);
for (int j = 1; j < (int)(state[i].size()); j++) {
int u = state[i][j - 1], v = state[i][j];
int lca = FindLCA(u, v);
// colour edges from u to lca and from v to lca
dp[u]++; dp[lca]--;
dp[v]++; dp[lca]--;
}
}
dfs2();
for (int i = 2; i <= n; i++) {
if (dp[i] > 0) {
DSUMerge(i, lift[i][0]);
}
}
// find degree of tree after merging
for (int i = 2; i <= n; i++) {
if (dp[i] == 0) {
deg[DSUFind(i)]++;
deg[DSUFind(lift[i][0])]++;
}
}
int leaves = 0;
for (int i = 1; i <= n; i++) {
leaves += (deg[i] == 1);
}
cout << (leaves + 1) / 2 << endl;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
16 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23864 KB |
Output is correct |
9 |
Correct |
13 ms |
23812 KB |
Output is correct |
10 |
Correct |
16 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23748 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
10 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23852 KB |
Output is correct |
17 |
Correct |
14 ms |
23772 KB |
Output is correct |
18 |
Correct |
12 ms |
23876 KB |
Output is correct |
19 |
Correct |
13 ms |
23816 KB |
Output is correct |
20 |
Correct |
12 ms |
23824 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23820 KB |
Output is correct |
23 |
Correct |
12 ms |
23836 KB |
Output is correct |
24 |
Correct |
15 ms |
23824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
16 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23864 KB |
Output is correct |
9 |
Correct |
13 ms |
23812 KB |
Output is correct |
10 |
Correct |
16 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23748 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
10 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23852 KB |
Output is correct |
17 |
Correct |
14 ms |
23772 KB |
Output is correct |
18 |
Correct |
12 ms |
23876 KB |
Output is correct |
19 |
Correct |
13 ms |
23816 KB |
Output is correct |
20 |
Correct |
12 ms |
23824 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23820 KB |
Output is correct |
23 |
Correct |
12 ms |
23836 KB |
Output is correct |
24 |
Correct |
15 ms |
23824 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24344 KB |
Output is correct |
27 |
Correct |
14 ms |
24276 KB |
Output is correct |
28 |
Correct |
16 ms |
24476 KB |
Output is correct |
29 |
Correct |
13 ms |
24308 KB |
Output is correct |
30 |
Correct |
17 ms |
24244 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
15 ms |
24532 KB |
Output is correct |
33 |
Correct |
14 ms |
23820 KB |
Output is correct |
34 |
Correct |
13 ms |
24260 KB |
Output is correct |
35 |
Correct |
14 ms |
24368 KB |
Output is correct |
36 |
Correct |
15 ms |
24220 KB |
Output is correct |
37 |
Correct |
14 ms |
24344 KB |
Output is correct |
38 |
Correct |
13 ms |
23828 KB |
Output is correct |
39 |
Correct |
13 ms |
24276 KB |
Output is correct |
40 |
Correct |
16 ms |
24244 KB |
Output is correct |
41 |
Correct |
17 ms |
24276 KB |
Output is correct |
42 |
Correct |
15 ms |
24332 KB |
Output is correct |
43 |
Correct |
17 ms |
24464 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
13 ms |
24404 KB |
Output is correct |
46 |
Correct |
17 ms |
24276 KB |
Output is correct |
47 |
Correct |
13 ms |
23820 KB |
Output is correct |
48 |
Correct |
14 ms |
24292 KB |
Output is correct |
49 |
Correct |
13 ms |
24276 KB |
Output is correct |
50 |
Correct |
12 ms |
24420 KB |
Output is correct |
51 |
Correct |
17 ms |
24344 KB |
Output is correct |
52 |
Correct |
15 ms |
24280 KB |
Output is correct |
53 |
Correct |
13 ms |
24296 KB |
Output is correct |
54 |
Correct |
12 ms |
24252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
16 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23864 KB |
Output is correct |
9 |
Correct |
13 ms |
23812 KB |
Output is correct |
10 |
Correct |
16 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23748 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
10 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23852 KB |
Output is correct |
17 |
Correct |
14 ms |
23772 KB |
Output is correct |
18 |
Correct |
12 ms |
23876 KB |
Output is correct |
19 |
Correct |
13 ms |
23816 KB |
Output is correct |
20 |
Correct |
12 ms |
23824 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23820 KB |
Output is correct |
23 |
Correct |
12 ms |
23836 KB |
Output is correct |
24 |
Correct |
15 ms |
23824 KB |
Output is correct |
25 |
Correct |
14 ms |
23828 KB |
Output is correct |
26 |
Correct |
82 ms |
39556 KB |
Output is correct |
27 |
Correct |
95 ms |
39228 KB |
Output is correct |
28 |
Correct |
15 ms |
24276 KB |
Output is correct |
29 |
Correct |
13 ms |
23848 KB |
Output is correct |
30 |
Correct |
12 ms |
23772 KB |
Output is correct |
31 |
Correct |
88 ms |
39160 KB |
Output is correct |
32 |
Correct |
13 ms |
24216 KB |
Output is correct |
33 |
Correct |
103 ms |
44920 KB |
Output is correct |
34 |
Correct |
101 ms |
39204 KB |
Output is correct |
35 |
Correct |
14 ms |
24332 KB |
Output is correct |
36 |
Correct |
90 ms |
39832 KB |
Output is correct |
37 |
Correct |
17 ms |
24276 KB |
Output is correct |
38 |
Correct |
14 ms |
24276 KB |
Output is correct |
39 |
Correct |
70 ms |
39372 KB |
Output is correct |
40 |
Correct |
16 ms |
24476 KB |
Output is correct |
41 |
Correct |
86 ms |
39144 KB |
Output is correct |
42 |
Correct |
115 ms |
41068 KB |
Output is correct |
43 |
Correct |
15 ms |
23824 KB |
Output is correct |
44 |
Correct |
121 ms |
45080 KB |
Output is correct |
45 |
Correct |
107 ms |
42196 KB |
Output is correct |
46 |
Correct |
14 ms |
24224 KB |
Output is correct |
47 |
Correct |
14 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
39552 KB |
Output is correct |
2 |
Correct |
58 ms |
41996 KB |
Output is correct |
3 |
Correct |
12 ms |
24276 KB |
Output is correct |
4 |
Correct |
13 ms |
24264 KB |
Output is correct |
5 |
Correct |
12 ms |
23820 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
16 ms |
24312 KB |
Output is correct |
8 |
Correct |
107 ms |
40940 KB |
Output is correct |
9 |
Correct |
18 ms |
24224 KB |
Output is correct |
10 |
Correct |
81 ms |
39668 KB |
Output is correct |
11 |
Correct |
14 ms |
23800 KB |
Output is correct |
12 |
Correct |
85 ms |
39684 KB |
Output is correct |
13 |
Correct |
82 ms |
40772 KB |
Output is correct |
14 |
Correct |
75 ms |
42064 KB |
Output is correct |
15 |
Correct |
65 ms |
39480 KB |
Output is correct |
16 |
Correct |
14 ms |
24276 KB |
Output is correct |
17 |
Correct |
16 ms |
23812 KB |
Output is correct |
18 |
Correct |
70 ms |
42396 KB |
Output is correct |
19 |
Correct |
109 ms |
45216 KB |
Output is correct |
20 |
Correct |
12 ms |
24284 KB |
Output is correct |
21 |
Correct |
14 ms |
23752 KB |
Output is correct |
22 |
Correct |
85 ms |
41080 KB |
Output is correct |
23 |
Correct |
15 ms |
24312 KB |
Output is correct |
24 |
Correct |
124 ms |
40016 KB |
Output is correct |
25 |
Correct |
86 ms |
44140 KB |
Output is correct |
26 |
Correct |
19 ms |
24284 KB |
Output is correct |
27 |
Correct |
13 ms |
24332 KB |
Output is correct |
28 |
Correct |
13 ms |
24352 KB |
Output is correct |
29 |
Correct |
16 ms |
24276 KB |
Output is correct |
30 |
Correct |
18 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
16 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23864 KB |
Output is correct |
9 |
Correct |
13 ms |
23812 KB |
Output is correct |
10 |
Correct |
16 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23748 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
10 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23852 KB |
Output is correct |
17 |
Correct |
14 ms |
23772 KB |
Output is correct |
18 |
Correct |
12 ms |
23876 KB |
Output is correct |
19 |
Correct |
13 ms |
23816 KB |
Output is correct |
20 |
Correct |
12 ms |
23824 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
11 ms |
23820 KB |
Output is correct |
23 |
Correct |
12 ms |
23836 KB |
Output is correct |
24 |
Correct |
15 ms |
23824 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24344 KB |
Output is correct |
27 |
Correct |
14 ms |
24276 KB |
Output is correct |
28 |
Correct |
16 ms |
24476 KB |
Output is correct |
29 |
Correct |
13 ms |
24308 KB |
Output is correct |
30 |
Correct |
17 ms |
24244 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
15 ms |
24532 KB |
Output is correct |
33 |
Correct |
14 ms |
23820 KB |
Output is correct |
34 |
Correct |
13 ms |
24260 KB |
Output is correct |
35 |
Correct |
14 ms |
24368 KB |
Output is correct |
36 |
Correct |
15 ms |
24220 KB |
Output is correct |
37 |
Correct |
14 ms |
24344 KB |
Output is correct |
38 |
Correct |
13 ms |
23828 KB |
Output is correct |
39 |
Correct |
13 ms |
24276 KB |
Output is correct |
40 |
Correct |
16 ms |
24244 KB |
Output is correct |
41 |
Correct |
17 ms |
24276 KB |
Output is correct |
42 |
Correct |
15 ms |
24332 KB |
Output is correct |
43 |
Correct |
17 ms |
24464 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
13 ms |
24404 KB |
Output is correct |
46 |
Correct |
17 ms |
24276 KB |
Output is correct |
47 |
Correct |
13 ms |
23820 KB |
Output is correct |
48 |
Correct |
14 ms |
24292 KB |
Output is correct |
49 |
Correct |
13 ms |
24276 KB |
Output is correct |
50 |
Correct |
12 ms |
24420 KB |
Output is correct |
51 |
Correct |
17 ms |
24344 KB |
Output is correct |
52 |
Correct |
15 ms |
24280 KB |
Output is correct |
53 |
Correct |
13 ms |
24296 KB |
Output is correct |
54 |
Correct |
12 ms |
24252 KB |
Output is correct |
55 |
Correct |
14 ms |
23828 KB |
Output is correct |
56 |
Correct |
82 ms |
39556 KB |
Output is correct |
57 |
Correct |
95 ms |
39228 KB |
Output is correct |
58 |
Correct |
15 ms |
24276 KB |
Output is correct |
59 |
Correct |
13 ms |
23848 KB |
Output is correct |
60 |
Correct |
12 ms |
23772 KB |
Output is correct |
61 |
Correct |
88 ms |
39160 KB |
Output is correct |
62 |
Correct |
13 ms |
24216 KB |
Output is correct |
63 |
Correct |
103 ms |
44920 KB |
Output is correct |
64 |
Correct |
101 ms |
39204 KB |
Output is correct |
65 |
Correct |
14 ms |
24332 KB |
Output is correct |
66 |
Correct |
90 ms |
39832 KB |
Output is correct |
67 |
Correct |
17 ms |
24276 KB |
Output is correct |
68 |
Correct |
14 ms |
24276 KB |
Output is correct |
69 |
Correct |
70 ms |
39372 KB |
Output is correct |
70 |
Correct |
16 ms |
24476 KB |
Output is correct |
71 |
Correct |
86 ms |
39144 KB |
Output is correct |
72 |
Correct |
115 ms |
41068 KB |
Output is correct |
73 |
Correct |
15 ms |
23824 KB |
Output is correct |
74 |
Correct |
121 ms |
45080 KB |
Output is correct |
75 |
Correct |
107 ms |
42196 KB |
Output is correct |
76 |
Correct |
14 ms |
24224 KB |
Output is correct |
77 |
Correct |
14 ms |
24276 KB |
Output is correct |
78 |
Correct |
98 ms |
39552 KB |
Output is correct |
79 |
Correct |
58 ms |
41996 KB |
Output is correct |
80 |
Correct |
12 ms |
24276 KB |
Output is correct |
81 |
Correct |
13 ms |
24264 KB |
Output is correct |
82 |
Correct |
12 ms |
23820 KB |
Output is correct |
83 |
Correct |
12 ms |
23764 KB |
Output is correct |
84 |
Correct |
16 ms |
24312 KB |
Output is correct |
85 |
Correct |
107 ms |
40940 KB |
Output is correct |
86 |
Correct |
18 ms |
24224 KB |
Output is correct |
87 |
Correct |
81 ms |
39668 KB |
Output is correct |
88 |
Correct |
14 ms |
23800 KB |
Output is correct |
89 |
Correct |
85 ms |
39684 KB |
Output is correct |
90 |
Correct |
82 ms |
40772 KB |
Output is correct |
91 |
Correct |
75 ms |
42064 KB |
Output is correct |
92 |
Correct |
65 ms |
39480 KB |
Output is correct |
93 |
Correct |
14 ms |
24276 KB |
Output is correct |
94 |
Correct |
16 ms |
23812 KB |
Output is correct |
95 |
Correct |
70 ms |
42396 KB |
Output is correct |
96 |
Correct |
109 ms |
45216 KB |
Output is correct |
97 |
Correct |
12 ms |
24284 KB |
Output is correct |
98 |
Correct |
14 ms |
23752 KB |
Output is correct |
99 |
Correct |
85 ms |
41080 KB |
Output is correct |
100 |
Correct |
15 ms |
24312 KB |
Output is correct |
101 |
Correct |
124 ms |
40016 KB |
Output is correct |
102 |
Correct |
86 ms |
44140 KB |
Output is correct |
103 |
Correct |
19 ms |
24284 KB |
Output is correct |
104 |
Correct |
13 ms |
24332 KB |
Output is correct |
105 |
Correct |
13 ms |
24352 KB |
Output is correct |
106 |
Correct |
16 ms |
24276 KB |
Output is correct |
107 |
Correct |
18 ms |
24276 KB |
Output is correct |
108 |
Correct |
551 ms |
110560 KB |
Output is correct |
109 |
Correct |
782 ms |
114844 KB |
Output is correct |
110 |
Correct |
879 ms |
123816 KB |
Output is correct |
111 |
Correct |
885 ms |
136704 KB |
Output is correct |
112 |
Correct |
711 ms |
128692 KB |
Output is correct |
113 |
Correct |
563 ms |
118292 KB |
Output is correct |
114 |
Correct |
606 ms |
100516 KB |
Output is correct |
115 |
Correct |
688 ms |
100520 KB |
Output is correct |
116 |
Correct |
840 ms |
106884 KB |
Output is correct |
117 |
Correct |
608 ms |
116380 KB |
Output is correct |
118 |
Correct |
729 ms |
101972 KB |
Output is correct |
119 |
Correct |
634 ms |
116536 KB |
Output is correct |
120 |
Correct |
833 ms |
127424 KB |
Output is correct |
121 |
Correct |
619 ms |
118256 KB |
Output is correct |
122 |
Correct |
823 ms |
112064 KB |
Output is correct |
123 |
Correct |
496 ms |
116060 KB |
Output is correct |
124 |
Correct |
843 ms |
116356 KB |
Output is correct |