http://acdream.info/problem?pid=1233

Andrew Stankevich's Contest (3)

ASC 3

Royal Federation

Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)

Problem Description

The king of Fooland has recently decided to reorganize his kingdom. Inspired by the democracy processes in neighbouring countries, he decided to convert his kingdom into Royal Federation. The Royal Federation would consist of several provinces, each headed by its governor.

There are N cities in his kingdom, numbered from 1 to N. Some cities are connected by roads. Roads are designed in such a way, that for each city there is exactly one way to get to any other city by the roads, not passing through any city more than once.

To prevent wastes for maintaining too small provinces, each province must contain at least B cities. However, to keep governments effective, each province must contain at most 3B cities.

Each province must have its governer headquaters in some city. This city may be outside the province itslef, but one must be able to get to the city with governer headquaters of his province in such a way, that all intermediate cities that he visits on his way belong to his province (and only the terminal city may be from another province).

One city may contain headquaters for several provinces.

Help the king to see his plans fulfilled.

Input

The first line of the input file contains two integer numbers - N and B (1 <= N <= 10 000, 1 <= B <= N).

Next N - 1 lines contain descriptions of roads, each line contains two integer numbers - the cities the road connects.

Output

If it is impossible to fulfil king's plans of reorganization, output 0 on the first line of the output file. In the other case output K - the number of provinces in your plan of the Royal Federation. After that output N integer numbers ranging from 1 to K - for each city output the number of the province it belongs to.

Finally output K integer numbers —— the cities where the capitals of the provinces must be located in.

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

Source

Andrew Stankevich Contest 3

Manager

题意:

国家有N个城市,任意城市可以到达任意城市,是一棵树。国王要给这些城市分省份。每个省份最少B个城市,最多3B个城市。每个省有一个首府,首府不一定是这个省的城市,只是首府到这个省各个城市只能经过这个省的城市。给出N和B,求分配方案,输出有多少个省,各个城市属于哪个省,每个省的首府是哪个城。(一个城可以是多个城的首府)(无解则输出0)

题解:

DFS,回溯时给当前点的各个子树分配颜色(省份),只要攒够不小于B个城市,就直接新生成一个颜色把它们刷了,首府是当前城市(所以可以当前点的不同分支刷成同一种颜色)。搞完所有点后,最后把剩下小于B个城市也刷成最后一种颜色。

正确性:主要就是看把最后剩下的小于B个城市也刷成最后一种颜色是否符合要求。根据我的刷法,一种颜色的城市数肯定是小于等于(2B-2)(两个含有B-1个城市的枝刷成的),最后剩下的没颜色的城市加上去也不会超过3B,OK!

然后是无解的情况,无解时颜色数为0,直接输出就行了,不用特判。

(这种范围比较宽的构造性题一般都比较水,大家居然都不做,搞的我也不敢做……

(其实一下也没想明白,慢慢才想出来的

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("huzhi.txt","w",stdout)
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back
const double pi=acos(-1.0);
const double eps=1e-; const int maxn=; struct Edge {
int v,next;
} e[maxn*];
int head[maxn],en; void addedge(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} int n,B; int f[maxn];
int sd[maxn];
int ans; inline void fil(const int &x , const int &fa , const int &ys) {
f[x]=ys;
for(int i=head[x]; i!=-; i=e[i].next)
if(e[i].v!=fa && f[e[i].v]==) fil(e[i].v , x , ys);
} bool used[maxn];
inline int dfs(const int &x) {
int i;
vector<pair<int,int> >s;
used[x]=;
for(i=head[x]; i!=-; i=e[i].next) {
if(used[e[i].v]==){
int t=dfs(e[i].v);
if(t!=)s.pb(mp(e[i].v , t));
}
}
int sum=,j=,k;
i=;
int size=s.size();
while(i<size) {
pair<int,int> pp=s[i];
sum+=pp.second;
if(sum>=B) {
ans++;
sd[ans]=x;
for(k=j; k<=i; k++) {
fil(s[k].first , x , ans);
sum-=s[k].second;
}
j=i+;
}
i++;
}
sum++;
if(sum>=B) {
ans++;
sd[ans]=x;
i=size-;
for(k=j; k<=i; k++) {
fil(s[k].first , x , ans);
sum-=s[k].second;
}
f[x]=ans;
sum--;
}
return sum;
} void farm() {
ans=;
mz(used);
mz(f);
int t=dfs();
if(f[]==)fil(,,ans);
} int main() {
int i,x,y;
while(RD2(n,B)!=EOF) {
en=;
mf1(head);
REP(i,n-) {
RD2(x,y);
addedge(x,y);
addedge(y,x);
}
farm();
printf("%d\n",ans);
if(ans!=) {
if(n>=)printf("%d",f[]);
FOR(i,,n)printf(" %d",f[i]);
puts("");
printf("%d",sd[]);
FOR(i,,ans)printf(" %d",sd[i]);
puts("");
}
}
return ;
}

acdream1233 Royal Federation (构造?)的更多相关文章

  1. 100198H Royal Federation

    传送门 题目大意 国家有N个城市,任意城市可以到达任意城市,是一棵树.国王要给这些城市分省份.每个省份最少M个城市,最多3M个城市.每个省有一个首府,首府不一定是这个省的城市,只是首府到这个省各个城市 ...

  2. 学习笔记:Maven构造版本号的方法解决浏览器缓存问题

    需要解决的问题 在做WEB系统开发时,为了提高性能会利用浏览器的缓存功能,其实即使不显式的申明缓存,现代的浏览器都会对静态文件(js.css.图片之类)缓存.但也正因为这个问题导致一个问题,就是资源的 ...

  3. 一步步构造自己的vue2.0+webpack环境

    前面vue2.0和webpack都已经有接触了些(vue.js入门,webpack入门之简单例子跑起来),现在开始学习如何构造自己的vue2.0+webpack环境. 1.首先新建一个目录vue-wk ...

  4. About 静态代码块,普通代码块,同步代码块,构造代码块和构造函数的纳闷

    构造函数用于给对象进行初始化,是给与之对应的对象进行初始化,它具有针对性,函数中的一种.特点:1:该函数的名称和所在类的名称相同.2:不需要定义返回值类型.3:该函数没有具体的返回值.记住:所有对象创 ...

  5. Eos开发——构造查询条件

    1.ajax 方式 var data = { orgid :orgid,year:year ,month: month,type:type,sortField:'sellEmpname' ,sortO ...

  6. 【C++】类和对象(构造与析构)

    类 类是一种抽象和封装机制,描述一组具有相同属性和行为的对象,是代码复用的基本单位. 类成员的访问权限 面向对象关键特性之一就是隐藏数据,采用机制就是设置类成员的访问控制权限.类成员有3种访问权限: ...

  7. Spring 设值注入 构造注入 p命名空间注入

    注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...

  8. 并发包的线程池第二篇--Executors的构造

    上一篇讲述了ThreadPoolExecutor的执行过程,我们也能看出来一个很明显的问题:这个线程池的构造函数比较复杂,对于不十分理解其运作原理的程序员,自己构造它可能体现和想象中不一样的行为.比如 ...

  9. 10、代码块、构造代码块、静态代码块及main方法之间的关系

    1.普通代码块: 在方法或语句中出现在{}之间的类容就称为普通代码块,简称代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出现先执行”,即顺序执行. /*下面第一个类时合法的 ...

随机推荐

  1. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  2. UOJ34 多项式乘法

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  3. Android成长日记-使用ToggleButton实现灯的开关

    案例演示 此案例实现思路:通过ToggleButton控件,ImageView控件实现 ---xml代码: <!-- textOn:true textOff:falase[s1] --> ...

  4. HTTP 长连接和短连接

    1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...

  5. php面向对象中static静态属性和静态方法的调用

    这篇文章主要介绍了php面向对象中static静态属性和静态方法的调用,实例分析了static静态属性和静态方法的原理与调用技巧,需要的朋友可以参考下 本文实例讲述了php中static静态属性和静态 ...

  6. centos安装altas

    centos6.6 atlas2.2.1 atlas项目 https://github.com/Qihoo360/Atlas/releases yum -y install gcc make ncur ...

  7. Swift Swift中的反射

    Swift的反射机制是基于一个叫 Mirror 的 struct 来实现的,其内部有如下属性和方法: let children: Children //对象的子节点. displayStyle: Mi ...

  8. (原)list中null的谨慎使用

    今天在刷算法题时,新建了ArrayList变量,发现ArrayList与null结合起来使用时经常出错. 请查看如下几种例子, 1.new一个ArrayList<>类型的数据, impor ...

  9. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-13: ordinal not i│ n range(128)

    python保持网页文件遇到的错误,归根结底还是编码问题,改一下要保存的数据为utf-8就好了. 如下最简单: import sys reload(sys) sys.setdefaultencodin ...

  10. Linux rsync网站目录同步功能的实现

    实现目标: 172.16.1.64服务器上的/var/www/sw_service目录,与172.16.1.60服务器上的/var/www/sw_service目录实现同步, 即1.60主动向1.64 ...