您的位置:首页 > 其它

[bzoj1086]王室联邦

2016-05-09 18:52 253 查看

题目描述

“余”人国的国王想重新编制他的国家。他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成

员来管理。他的国家有n个城市,编号为1..n。一些城市之间有道路相连,任意两个不同的城市之间有且仅有一条

直接或间接的道路。为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个

城市。每个省必须有一个省会,这个省会可以位于省内,也可以在该省外。但是该省的任意一个城市到达省会所经

过的道路上的城市(除了最后一个城市,即该省省会)都必须属于该省。一个城市可以作为多个省的省会。聪明的

你快帮帮这个国王吧!

greedy

由于我太弱了就去orz了hzwer的题解

无解情况是n<b。

接下来我们来弄一个贪心。

贪心方法见代码。

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=1000+10;
int h[maxn],go[maxn*2],next[maxn*2],size[maxn],belong[maxn],cap[maxn],s[maxn];
int i,j,k,l,t,n,m,c,tot,top,cnt;
void add(int x,int y){
go[++tot]=y;
next[tot]=h[x];
h[x]=tot;
}
void dfs(int x,int y){
s[++top]=x;
int t=h[x];
while (t){
if (go[t]!=y){
dfs(go[t],x);
size[x]+=size[go[t]];
if (size[x]>=c){
size[x]=0;
cap[++cnt]=x;
while (s[top]!=x) belong[s[top--]]=cnt;
}
}
t=next[t];
}
size[x]++;
}
void dg(int x,int y,int z){
if (belong[x]) z=belong[x];else belong[x]=z;
int t=h[x];
while (t){
if (go[t]!=y) dg(go[t],x,z);
t=next[t];
}
}
int main(){
scanf("%d%d",&n,&c);
if (n<c){
printf("0\n");
return 0;
}
fo(i,1,n-1){
scanf("%d%d",&j,&k);
add(j,k);add(k,j);
}
dfs(1,0);
if (!cnt) cap[cnt=1]=1;
dg(1,0,cnt);
printf("%d\n",cnt);
fo(i,1,n) printf("%d%c",belong[i],(i==n)?'\n':' ');
fo(i,1,cnt) printf("%d%c",cap[i],(i==cnt)?'\n':' ');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: