题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1086

一眼看去很是不会,于是看看TJ...

https://blog.csdn.net/lych_cys/article/details/50165643

也就是这样啦...只要以自己为省会,就可以连接零散的儿子,用栈存下儿子中的零散点即可;

因为尽量让儿子自己成为一个省,所以零散的部分一定和自己相连,到时候就可以连在一起;

最后还剩下的零散的点一定 <= b+1,否则会出去一次;

最后一个省的大小一定 <= 2*b+1,因为一旦满足就成为一个省,最差也就是 b-1 + b-1 + 1 成为一个省;

所以最后剩下的点连进最后一个省即可;

然后...只有 b>n 的时候无解?

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=;
int n,b,sta[xn],top,cnt,col[xn],s[xn],hd[xn],ct,to[xn<<],nxt[xn<<];
void add(int x,int y){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct;}
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
void dfs(int x,int fa)
{
int pr=top;
for(int i=hd[x],u;i;i=nxt[i])
{
if((u=to[i])==fa)continue;
dfs(u,x);
if(top-pr>=b)
{
cnt++; s[cnt]=x;//x !u
while(top>pr)col[sta[top]]=cnt,top--;
}
}
sta[++top]=x;
}
int main()
{
n=rd(); b=rd();
for(int i=,x,y;i<n;i++)x=rd(),y=rd(),add(x,y),add(y,x);
dfs(,); while(top)col[sta[top]]=cnt,top--;
printf("%d\n",cnt);
for(int i=;i<=n;i++)printf("%d ",col[i]); printf("\n");
for(int i=;i<=cnt;i++)printf("%d ",s[i]); printf("\n");
return ;
}

最新文章

  1. String 中去掉空格
  2. 在CSS中通过@font-face属性来实现网页中嵌入特殊字体。
  3. 几个有用的JSON工具
  4. HTTP的报文格式,GET和POST的区别
  5. python 间谍程序传输文件 socket编程
  6. 第十六周oj刷题——Problem J: 填空题:静态成员---计算学生个数
  7. Mapreduce参数调节
  8. supermap开发webgis的经验
  9. maven使用私服以后,Missing artifact xxx:xxx:jar:xx的问题
  10. 【BZOJ1003】物流运输(动态规划,最短路)
  11. Unity User Group深圳站——Timeline &amp; Cinemachine分享
  12. 滤波器——BoxBlur均值滤波及其快速实现
  13. Huawei BGP和OSPF双边界重分布(二)
  14. 机器学习:K-Means/K-Means++
  15. Lab 1-2
  16. 获取Javascript 滚动条距离顶部的距离(兼容IE6+,火狐,谷歌,其它没测)
  17. Valid Parentheses &amp; Longest Valid Parentheses
  18. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
  19. 对position的认知观
  20. CentOS7下 Python2.7.5升级为Python2.7.13

热门文章

  1. 【BZOJ1834】network 网络扩容(最大流,费用流)
  2. No route info of this topic
  3. DNS Prefetch 【DNS 预解析技术】
  4. XCode 或者ITune 添加账号时,提示:This action could not be completed. 或者 Access Privileges
  5. Java面试题总结之OOA/D,UML,和XML
  6. zookeeper原理浅析(二)
  7. 转:TLV 格式及编解码示例
  8. Atom安装代码格式化插件atom-beautify
  9. 分布式 OLTP 数据库
  10. 【Nginx】Nginx的配置