学习树分块的正确姿势,可惜ljoj上没有spj

//By Richard
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#define rep(x,y,z) for (int x=(y);(x)<=(z);(x)++)
#define per(x,y,z) for (int x=(y);(x)>=(z);(x)--)
#define log2(x) (31-__builtin_clz(x))
#define mod (int)(1e9+7)
#define inf 0x3f3f3f3f
#define cls(x) memset(x,0,sizeof(x))
#ifdef DEBUG
#define debugdo(X) X
#define debugndo(X)
#define debugout(X) cout<<(#X)<<"="<<(X)<<endl
#else
#define debugdo(X)
#define debugndo(X) X
#define debugout(X)
#endif // debug
#ifdef ONLINE_JUDGE
#define debugdo(X)
#define debugndo(X)
#define debugout(X)
#endif
#define putarray(x,n) rep(iiii,1,n) printf("%d ",x[iiii])
#define mp make_pair
using namespace std;
typedef pair<int,int> pairs;
typedef long long LL;
/////////////////////read4.0////////////////////////////////////
template <typename T>
inline void read(T &x){char ch;x=0;bool flag=false;ch=getchar();while (ch>'9'||ch<'0') {if (ch=='-') flag=true;ch=getchar();}while ((ch<='9'&&ch>='0')){x=x10+ch-'0';ch=getchar();}if (flag) x=-1;}
template <typename T>
inline void read(T &x,T &y){read(x);read(y);}
template <typename T>
inline void read(T &x,T &y,T &z){read(x);read(y);read(z);}
/////////////////variables&functions////////////////////
const int maxn=1111;
int n,b,first[maxn],nxt[maxn2],to[maxn2],cnt,belong[maxn],tot,capital[maxn],stack[maxn],top;
bool vis[maxn];
inline void addedge(int u,int v)
{
nxt[++cnt]=first[u];
first[u]=cnt;
to[cnt]=v;

nxt[++cnt]=first[v];
first[v]=cnt;
to[cnt]=u;

}
void dfs(int step,int x)
{
vis[x]=true;int bot=top;
for (int q=first[x];q;q=nxt[q])
{
if (!vis[to[q]])
{
dfs(step+1,to[q]);
if(top-bot>=b)
{
capital[++tot]=x;
while(top!=bot) belong[stack[top–]]=tot;
}
}
}
stack[++top]=x;
}
int main()
{
read(n,b);
rep(i,1,n-1)
{
int x,y;
read(x,y);
addedge(x,y);
}
dfs(0,1);
while (top) belong[stack[top–]]=tot;
printf("%d\n",tot);
rep(i,1,n) printf("%d ",belong[i]);
putchar(‘\n’);
rep(i,1,tot) printf("%d ",capital[i]);
return 0;
}