# 【bzoj1086】 scoi2005—王室联邦

### 题意

求将树分为几个联通块，每个联通块大小大于B小于3B，是否可行。

### 代码

// bzoj3757
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define MOD 1000000007
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}

const int maxn=1010;
struct edge {int next,to;}e[maxn<<2];

void insert(int u,int v) {
}
void dfs(int x,int fa) {
q[++top]=x;
for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) {
dfs(e[i].to,x);
if (size[x]+size[e[i].to]>=B) {
size[x]=0;
cap[++cnt]=x;
while (q[top]!=x) pos[q[top--]]=cnt;
}
else size[x]+=size[e[i].to];
}
size[x]++;
}
void paint(int x,int fa,int c) {
if (pos[x]) c=pos[x];
else pos[x]=c;
for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) paint(e[i].to,x,c);
}
int main() {
scanf("%d%d",&n,&B);
for (int i=1;i<n;i++) {
int u,v;
scanf("%d%d",&u,&v);
insert(u,v);
}
if (n<B) {printf("0");return 0;}
cnt=0;dfs(1,0);
if (!cnt) cap[++cnt]=1;
paint(1,0,cnt);
printf("%d\n",cnt);
for (int i=1;i<=n;i++) printf("%d ",pos[i]);
printf("\n");
for (int i=1;i<=cnt;i++) printf("%d ",cap[i]);
return 0;
}


posted @ 2016-09-27 22:34  MashiroSky  阅读(318)  评论(0编辑  收藏  举报