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 ...
随机推荐
- hibernate简介以及简单配置
Hibernate简介: Hibernate是一个开源对象关联关系映射的框架,他对JDBC做了轻量级的封装,使我们可以通过面向对象的思想操作数据库. 为什么要用Hibernate: 1: 对JDBC访 ...
- Vim如何显示和关闭行号
显示行号: set nu 去除行号: set nonu
- dict 方法总结整理
#!/usr/bin/env python __author__ = "lrtao2010" #Python 3.7.0 字典常用方法 #字典的key是唯一的,且不能被修改,val ...
- 安装VS2010 无法打开数据文件deffactory.dat
VS2010旗舰版可用Key: YCFHQ9DWCYDKV88T2TMHG7BHP 解压VS2010安装ISO文件,找到setup\deffactory.dat文件,用记事本打开,将里面内容清空,将以 ...
- Django基于Pycharm开发之二 [使用django adminSite]
在使用django自带的adminsite的时候,有以下内容需要做. 1.数据迁移,管理表的创建. 2.启用本地化 (setting.py的配置) 一.数据迁移,默认情况下,安装django之后,dj ...
- MySQL数据库详解(一)执行SQL查询语句时,其底层到底经历了什么?
一条SQL查询语句是如何执行的? 前言 大家好,我是WZY,今天我们学习下MySQL的基础框架,看一件事千万不要直接陷入细节里,你应该先鸟瞰其全貌,这样能够帮助你从高维度理解问题.同样,对于MyS ...
- Leetcode 472.连接词
连接词 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词. 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的. 示例: 输入: ["cat" ...
- [python学习篇][书籍学习][python standrad library][内建类型]之数值
数值类型 — int, float, long, complex 有四种不同的数值类型:普通整数.长整数.浮点数和复数 普通整数(或者简称整数)使用C中的long实现,其精度至少为32位(sys.ma ...
- [python学习篇] uiautomator xiaocong
Skip to content This repository Pull requests Issues Marketplace Gist Sign out Watch103 ...
- [git 学习篇] 提交文件
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...