这一篇类似是强联通的东东大家参考一下吧,我也不会讲这题。
大家可以传送去我同学那里看看的QaQ传送门
提交传送门
题目描述
“余”人国的国王想重新编制他的国家。他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理。
他的国家有n个城市,编号为1..n。一些城市之间有道路相连,任意两个不同的城市之间有且仅有一条直接或间接的道路。为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个城市。
每个省必须有一个省会,这个省会可以位于省内,也可以在该省外。但是该省的任意一个城市到达省会所经过的道路上的城市(除了最后一个城市,即该省省会)都必须属于该省。一个城市可以作为多个省的省会。
聪明的你快帮帮这个国王吧!
输入文件:
第一行包含两个数N,B(1<=N<=1000, 1 <= B <= N)。接下来N-1行,每行描述一条边,包含两个数,即这条边连接的两个城市的编号。
输出文件:
如果无法满足国王的要求,输出0。否则第一行输出数K,表示你给出的划分方案中省的个数,编号为1..K。第二行输出N个数,第I个数表示编号为I的城市属于的省的编号,第三行输出K个数,表示这K个省的省会的城市编号,如果有多种方案,你可以输出任意一种。
Sample Input
8 2
1 2
2 3
1 8
8 7
8 6
4 6
6 5
Sample Output
3
2 1 1 3 3 3 3 2
2 1 8
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int x,y,next;
}a[21000];int len,first[11000];
void ins(int x,int y){
len++;
a[len].x=x;a[len].y=y;
a[len].next=first[x];first[x]=len;
}
int sta[11000],top;
int belong[11000],tot;
int rt[11000],n,L;
void dfs(int fa,int x){
int now=top;
for(int k=first[x];k>0;k=a[k].next){
int y=a[k].y;
if(y!=fa){
dfs(x,y);
if(top-now>=L){
rt[++tot]=x;
while(top!=now){
belong[sta[top--]]=tot;
}
}
}
}
sta[++top]=x;
}
int main(){
scanf("%d%d",&n,&L);
if(n<L){printf("0\n");return 0;}
for(int i=1;i<n;i++){
int x,y;scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
dfs(0,1);
while(top>0)belong[sta[top--]]=tot;
printf("%d\n",tot);
for(int i=1;i<n;i++)printf("%d ",belong[i]);printf("%d\n",belong[n]);
for(int i=1;i<tot;i++)printf("%d ",rt[i]);printf("%d\n",rt[tot]);
return 0;
}