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

8 2
1 2
2 3
1 8
8 7
8 6
4 6
6 5

Sample Output

3
2 1 1 3 3 3 3 2
2 1 8
 
 
思路:
  分块+dfs
 
来,上代码:

#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的更多相关文章

  1. AC日记——[Hnoi2017]影魔 bzoj 4826

    4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...

  2. AC日记——[LNOI2014]LCA bzoj 3626

    3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...

  3. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  4. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  5. AC日记——[HNOI2014]世界树 bzoj 3572

    3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...

  6. AC日记——NOI2016区间 bzoj 4653

    4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...

  7. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  8. AC日记——[HNOI2008]越狱 bzoj 1008

    1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...

  9. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

随机推荐

  1. 【mysql】【转发】Cannot proceed because system tables used by Event Scheduler were found damaged at server start

    本地:mac 10.12.3  mysql 5.6   远程:linux 7.3    mysql 5.7.18.  (远程数据库yum安装,又5.6升级到5.7)   步骤:从本地数据库导出数据到远 ...

  2. 图解Disruptor框架(二):核心概念

    图解Disruptor框架(二):核心概念 概述 上一个章节简单的介绍了了下Disruptor,这节就是要好好的理清楚Disruptor中的核心的概念.并且会给出个HelloWorld的小例子. 在正 ...

  3. python 程序小测试

    python 程序小测试 对之前写的程序做简单的小测试 ... # -*- encoding:utf-8 -*- ''' 对所写程序做简单的测试 @author: bpf ''' def GameOv ...

  4. matplotlib学习记录 三

    # 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...

  5. 20181210(os,os.path,subprocess,configparser,shutil)

    1.os模块 os表示操作系统,该模块主要处理与操作系统相关的操作.最常用的是文件操作:打开,读取,删除,复制,重命名. 重点掌握增删改查的函数操作. import os# 获取当前执行文件所在文件夹 ...

  6. 学习ucosii要用到的几本书

    转自:http://bbs.elecfans.com/jishu_551275_1_1.html   1.嵌入式实时操作系统μC/OS-II(第2版)  邵贝贝 等译 北京航空航天大学出版社     ...

  7. Mysql新建数据库、删除数据库

    新建数据库 create database db_name; //db_name为新建数据库的名字 mysql> create database db_name; Query OK, row a ...

  8. BZOJ 2687: 交与并

    答案存在于 1.两个互相包含的区间 2.两个互不包含的区间 决策单调性显然 但是这代码很精妙啊,并不知道这个为什么能这样写 #include<cstdio> #include<alg ...

  9. JAVA-基础(十) Swing

    在看到applet和Swing的时候,我想起了winform,以及java beans包中各种所谓的组件的时候,一切都那么似曾相识. Swing是AWT的扩展,它提供了更强大和更灵活的组件集合. 除了 ...

  10. 如何用treap写luogu P3391

    treap大法好!!! splay什么的都是异端 --XZZ 先%FHQ为敬 (fhq)treap也是可以搞区间翻转的 每次把它成(1~L-1)(L~R)(R+1~n)三块然后打标记拼回去 对于有标记 ...