AC日记——王室联邦 bzoj 1086
Description
“余”人国的国王想重新编制他的国家。他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成
员来管理。他的国家有n个城市,编号为1..n。一些城市之间有道路相连,任意两个不同的城市之间有且仅有一条
直接或间接的道路。为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个
城市。每个省必须有一个省会,这个省会可以位于省内,也可以在该省外。但是该省的任意一个城市到达省会所经
过的道路上的城市(除了最后一个城市,即该省省会)都必须属于该省。一个城市可以作为多个省的省会。聪明的
你快帮帮这个国王吧!
Input
第一行包含两个数N,B(1<=N<=1000, 1 <= B <= N)。接下来N-1行,每行描述一条边,包含两个数,即这
条边连接的两个城市的编号。
Output
如果无法满足国王的要求,输出0。否则输出数K,表示你给出的划分方案中省的个数,编号为1..K。第二行输
出N个数,第I个数表示编号为I的城市属于的省的编号,第三行输出K个数,表示这K个省的省会的城市编号,如果
有多种方案,你可以输出任意一种。
Sample Input
1 2
2 3
1 8
8 7
8 6
4 6
6 5
Sample Output
2 1 1 3 3 3 3 2
2 1 8
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 1001 using namespace std; struct EdgeType {
int to,next;
};
struct EdgeType edge[maxn<<]; int head[maxn],cnt,n,belong[maxn],root[maxn],b;
int stack[maxn],top; inline void edge_add(int from,int to)
{
edge[++cnt].to=to;
edge[cnt].next=head[from];
head[from]=cnt;
} void search(int now,int fa)
{
int bottom=top;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].to==fa) continue;
search(edge[i].to,now);
if(top-bottom>=b)
{
root[++cnt]=now;
while(top!=bottom)
{
belong[stack[top--]]=cnt;
}
}
}
stack[++top]=now;
} int main()
{
scanf("%d%d",&n,&b);
int from,to;
for(int i=;i<n;i++)
{
scanf("%d%d",&from,&to);
edge_add(from,to);
edge_add(to,from);
}
cnt=,search(,);
while(top) belong[stack[top--]]=cnt;
printf("%d\n",cnt);
for(int i=;i<=n;i++)
{
printf("%d ",belong[i]);
}
printf("\n");
for(int i=;i<=cnt;i++)
{
printf("%d ",root[i]);
}
return ;
}
AC日记——王室联邦 bzoj 1086的更多相关文章
- AC日记——[Hnoi2017]影魔 bzoj 4826
4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...
- AC日记——[LNOI2014]LCA bzoj 3626
3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...
- AC日记——[ZJOI2012]网络 bzoj 2816
2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...
- AC日记——[SCOI2009]游戏 bzoj 1025
[SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...
- AC日记——[HNOI2014]世界树 bzoj 3572
3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...
- AC日记——NOI2016区间 bzoj 4653
4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...
- AC日记——Rmq Problem bzoj 3339
3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——[HNOI2008]越狱 bzoj 1008
1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...
- AC日记——[FJOI2007]轮状病毒 bzoj 1002
1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...
随机推荐
- 模拟发送http请求的工具推荐
做网站开发时,经常需要发送请求来测试自己的代码是否OK,这时候模拟发送http请求的工具就起到了很大的作用.特别是需要在请求带header时就更加的有必要使用工具.下面推荐的工具有的是基于系统开发的程 ...
- 科学计算库Numpy——概述
Numpy主要用于数组的各种计算. 导入Numpy import numpy as np 数组类型 Numpy的数组类型为numpy.ndarray. array=np.array([1,2,3,4, ...
- B - CD UVA - 624
https://cn.vjudge.net/contest/224070#problem/B #include <iostream> #include <cstring> #i ...
- git push后是空目录,且提示modified content, untracked content
最近往自己的github传代码时,每一步都正常,但最后push上去之后是空目录,且在本地执行git status时提示: 后来发现是由于push的工程下本来就有个.git目录,所以才导致push上去的 ...
- Python虚拟机之异常控制流(五)
Python中的异常控制语义结构 在Python虚拟机之异常控制流(四)这一章中,我们考察了Python的异常在虚拟机中的级别上是什么东西,抛出异常这个动作在虚拟机的级别上对应的行为,最后,我们还剖析 ...
- 豆邮windows客户端(第三方)开发详解
“豆邮”,是社区网站“豆瓣”的一个类似私信的功能模块.在豆瓣官网,“豆邮”曾一度被改为“私信”,但在遭到众多豆瓣用户的强烈反对之后又改了回来.然而,在豆瓣的移动客户端上,仍称呼为“私信”. 豆邮的设定 ...
- 06-python进阶-多线程下载器练手
我们需要用python 写一个多线程的下载器 我们要先获取这个文件的大小 然后将其分片 然后启动多线程 分别去下载 然后将其拼接起来 #!/usr/bin/env python#coding:utf- ...
- bootstrap3兼容ie8浏览器
bootstrap3 兼容IE8浏览器 2016-01-22 14:01 442人阅读 评论(0) 收藏 举报 分类: html5(18) 目录(?)[+] 近期在使用bootstrap这 ...
- read(byte[] b)与readFully(byte[] b)
转载于:http://yyzjava.iteye.com/blog/1178525 要搞清楚read(byte[] b)和readFully(byte[] b)的区别,可以从以下方面着手分析: 1.代 ...
- iOS-----openGL--openGL ES iOS 入门篇--->搭建openGL环境
OpenGL版本 iOS系统默认支持OpenGl ES1.0.ES2.0以及ES3.0 3个版本,三者之间并不是简单的版本升级,设计理念甚至完全不同,在开发OpenGL项目前,需要根据业务需求选择合适 ...