[SCOI2005]王室联邦

时间:2021-10-03 15:50:56

分块基本没有限制
所以每次大于等于b就分一块

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e3 + 10); IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
} int b, n, fst[_], num, nxt[_], to[_], cnt, bel[_], S[_], rt[_]; IL void Add(RG int u, RG int v){ to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++; } IL void Dfs(RG int u, RG int fa){
RG int x = S[0];
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(to[e] == fa) continue;
Dfs(to[e], u);
if(S[0] - x >= b){
rt[++num] = u;
while(S[0] != x) bel[S[S[0]--]] = num;
}
}
S[++S[0]] = u;
} int main(RG int argc, RG char *argv[]){
n = Read(); b = Read(); Fill(fst, -1);
for(RG int i = 1, u, v; i < n; i++) u = Read(), v = Read(), Add(u, v), Add(v, u);
if(n < b){ puts("0"); return 0; }
Dfs(1, 0);
while(S[0]) bel[S[S[0]--]] = num;
printf("%d\n", num);
for(RG int i = 1; i <= n; i++) printf("%d ", bel[i]);
printf("\n");
for(RG int i = 1; i <= num; i++) printf("%d ", rt[i]);
printf("\n");
return 0;
}