给树分块即可,为树上分块 / 树上莫队做铺垫。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// ==============================
// author: memset0
// website: https://memset0.cn
// ==============================
#include <bits/stdc++.h>
#define ll long long

int read() {
int x = 0; bool m = 0; char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') m = 1, c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (m) return -x; else return x;
}

const int maxn = 1010;

int n, m, u, v, pos, top;
int ans[maxn], stk[maxn], center[maxn];

int tot = 2, hed[maxn], nxt[maxn << 1], to[maxn << 1];
void add_edge(int u, int v) { nxt[tot] = hed[u], to[tot] = v, hed[u] = tot++; }

void dfs(int u, int father) {
int bottom = top;
for (int i = hed[u], v = to[i]; i; i = nxt[i], v = to[i])
if (v != father) {
dfs(v, u);
if (top - bottom >= m) {
++pos, center[pos] = u;
while (top > bottom) {
int v = stk[top--];
ans[v] = pos;
}
}
}
stk[++top] = u;
}

int main() {

n = read(), m = read();
for (int i = 1; i < n; i++) {
u = read(), v = read();
add_edge(u, v);
add_edge(v, u);
}

dfs(1, 0);
if (top) {
while (top) {
int v = stk[top--];
ans[v] = pos;
}
}

printf("%d\n", pos);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
puts("");
for (int i = 1; i <= pos; i++)
printf("%d ", center[i]);
puts("");

return 0;
}