bzoj 1086 王室联邦 —— 思路题
2024-09-02 08:31:14
题目: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 ;
}
最新文章
- String 中去掉空格
- 在CSS中通过@font-face属性来实现网页中嵌入特殊字体。
- 几个有用的JSON工具
- HTTP的报文格式,GET和POST的区别
- python 间谍程序传输文件 socket编程
- 第十六周oj刷题——Problem J: 填空题:静态成员---计算学生个数
- Mapreduce参数调节
- supermap开发webgis的经验
- maven使用私服以后,Missing artifact xxx:xxx:jar:xx的问题
- 【BZOJ1003】物流运输(动态规划,最短路)
- Unity User Group深圳站——Timeline &; Cinemachine分享
- 滤波器——BoxBlur均值滤波及其快速实现
- Huawei BGP和OSPF双边界重分布(二)
- 机器学习:K-Means/K-Means++
- Lab 1-2
- 获取Javascript 滚动条距离顶部的距离(兼容IE6+,火狐,谷歌,其它没测)
- Valid Parentheses &; Longest Valid Parentheses
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
- 对position的认知观
- CentOS7下 Python2.7.5升级为Python2.7.13
热门文章
- 【BZOJ1834】network 网络扩容(最大流,费用流)
- No route info of this topic
- DNS Prefetch 【DNS 预解析技术】
- XCode 或者ITune 添加账号时,提示:This action could not be completed. 或者 Access Privileges
- Java面试题总结之OOA/D,UML,和XML
- zookeeper原理浅析(二)
- 转:TLV 格式及编解码示例
- Atom安装代码格式化插件atom-beautify
- 分布式 OLTP 数据库
- 【Nginx】Nginx的配置