传送门

题目大意

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

分析

我们不难想到先进行dfs,将每一块满足要求的划分出来,然后再将某个点的儿子中没被划分出来的点为根的子树几个拼成一个,使得它也成为合法的一个省。但是我们再想一想可以发现,这样划分最后可能会多出一些没有归属的点,而这些点最多为M-1个,所以我们只需要控制之前划分出来的每个省城市数不超过2M+1,然后最后将没有归属的省加到最后一个建立的省就行了。详见代码。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack> using namespace std; #define ct cout
#define el endl
#define fi first
#define se second
#define pf printf
#define li long long
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define y1 y12345678909
#define rii register int
#define pii pair<int,int>
#define ck(x) cout<<x<<endl;
#define uli unsigned long long
#define clr(x) memset(x,0,sizeof(x))
#define sp cout<<"---------------------------------------------------"<<endl inline int ra(){
int _x=,_f=;char _s=getchar();
while(!isdigit(_s)){if(_s=='-')_f=-;_s=getchar();}
while(isdigit(_s)){_x=(_x<<)+(_x<<)+(_s-'');_s=getchar();}
return _x*_f;
}
inline li lra(){
li _x=,_f=;char _s=getchar();
while(!isdigit(_s)){if(_s=='-')_f=-;_s=getchar();}
while(isdigit(_s)){_x=(_x<<)+(_x<<)+(_s-'');_s=getchar();}
return _x*_f;
} //---------------------------------------------------------------------------//
//---------------------------------------------------------------------------// vi v[];
int siz[],n,m,sum,belong[],head[];
inline void cor(int x,int col,int fa){
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa&&!belong[v[x][i]]){
belong[v[x][i]]=col;
cor(v[x][i],col,x);
}
return;
}
inline void dfs(int x,int fa){
int i,j,k;
siz[x]=;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa){
dfs(v[x][i],x);
if(!belong[v[x][i]])siz[x]+=siz[v[x][i]];
}
if(siz[x]>=m&&siz[x]<=*m+){
head[++sum]=x;
cor(x,sum,fa);
belong[x]=sum;
return;
}
if(siz[x]<m)return;
int ns=,la=-;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa&&!belong[v[x][i]]){
ns+=siz[v[x][i]];
if(ns>=m){
head[++sum]=x;
for(int j=la+;j<=i;j++)
if(v[x][j]!=fa&&!belong[v[x][j]]){
belong[v[x][j]]=sum;
cor(v[x][j],sum,x);
}
la=i;
ns=;
}
}
for(int i=la+;i<v[x].size();i++)
if(v[x][i]!=fa&&!belong[v[x][i]]){
belong[v[x][i]]=sum;
cor(v[x][i],sum,x);
}
belong[x]=sum;
return;
}
int main(){
freopen("royal.in","r",stdin);
freopen("royal.out","w",stdout);
clr(belong);
clr(siz);
clr(head);
int i,j,k;
n=ra(),m=ra();
for(i=;i<n;i++){
int x,y;
x=ra(),y=ra();
v[x].pb(y),v[y].pb(x);
}
dfs(,);
pf("%d\n",sum);
if(sum){
for(i=;i<=n;i++)
if(!belong[i])
belong[i]=sum;
for(i=;i<=n;i++)pf("%d ",belong[i]);
puts("");
for(i=;i<=sum;i++)pf("%d ",head[i]);
puts("");
}
return ;
}

100198H Royal Federation的更多相关文章

  1. acdream1233 Royal Federation (构造?)

    http://acdream.info/problem?pid=1233 Andrew Stankevich's Contest (3) ASC 3 Royal Federation Special ...

  2. HDFS Federation (读书笔记)

    HDFS Federation (读书笔记) HDFS的架构 HDFS包含两个层次:命名空间管理(Namespace) 和 块/存储管理(Block Storage). 命名空间管理(Namespac ...

  3. 国内第一篇详细讲解hadoop2的automatic HA+Federation+Yarn配置的教程

    前言 hadoop是分布式系统,运行在linux之上,配置起来相对复杂.对于hadoop1,很多同学就因为不能搭建正确的运行环境,导致学习兴趣锐减.不过,我有免费的学习视频下载,请点击这里. hado ...

  4. [zz]The Royal Treatment

    http://www.cgw.com/Publications/CGW/2012/Volume-35-Issue-4-June-July-2012/The-Royal-Treatment.aspx T ...

  5. hadoop2的automatic HA+Federation+Yarn配置的教程

    前言 hadoop是分布式系统,运行在linux之上,配置起来相对复杂.对于hadoop1,很多同学就因为不能搭建正确的运行环境,导致学习兴趣锐减.不过,我有免费的学习视频下载,请点击这里. hado ...

  6. Add And Reset a Federation Server to a Federation Server Farm adfs ad

    Applies To: Active Directory Federation Services (AD FS) 2.0 After you install the Active Directory ...

  7. Configuring Active Directory Federation Services 2.0 (配置 adfs 2.0) -摘自网络

    Active Directory Federation Services (AD FS) 2.0 makes it possible to deploy a federation server and ...

  8. Hadoop 学习笔记 (十) hadoop2.2.0 生产环境部署 HDFS HA Federation 含Yarn部署

    其他的配置跟HDFS-HA部署方式完全一样.但JournalNOde的配置不一样>hadoop-cluster1中的nn1和nn2和hadoop-cluster2中的nn3和nn4可以公用同样的 ...

  9. HDFS Federation与HDFS High Availability详解

    HDFS Federation NameNode在内存中保存文件系统中每个文件和每个数据块的引用关系,这意味着对于一个拥有大量文件的超大集群来说,内存将成为限制系统横向扩展的瓶颈.在2.0发行版本系列 ...

随机推荐

  1. 剑指Offer-翻转单词顺序

    题目描述: 输入一个英文句子,翻转句子中单词的顺序,但单词内字符串的顺序不变.例如输入字符串:"I am a student",则输出"student a am I&qu ...

  2. python发短信功能

    http://www.cnblogs.com/martianShu/p/5847289.html

  3. python+rabbitmq实现分布式

    #master # -*- coding: utf-8 -*-import sys#reload(sys)sys.setdefaultencoding("utf-8") impor ...

  4. 伪元素after,before,css/js控制样式

    CSS<style> body { font: 200%/1.45 charter; } ref::before { content: '\00A7'; letter-spacing: . ...

  5. sourcetree 安装与操作

    sourcetree操作 http://www.jianshu.com/p/be9f0484af9d SourceTree安装教程和GitLab配置详解 http://www.cnblogs.com/ ...

  6. [转载]create_proc_read_entry中函数的说明

    原型: struct proc_dir_entry *create_proc_read_entry (const char *name, mode_t mode, struct proc_dir_en ...

  7. L2-023. 图着色问题(暴力)

    L2-023. 图着色问题 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 图着色问题是一个著名的NP完全问题.给定无向图 G ...

  8. AngularJS:SQL

    ylbtech-AngularJS:SQL 1.返回顶部 1. AngularJS SQL 在前面章节中的代码也可以用于读取数据库中的数据. 使用 PHP 从 MySQL 中获取数据 AngularJ ...

  9. AngularJS:应用

    ylbtech-AngularJS:应用 1.返回顶部 1. AngularJS 应用 现在是时候创建一个真正的 AngularJS 单页 Web 应用(single page web applica ...

  10. PowerDesigner CDM中取消默认不能存在同名主键的方法

    This data item is already used in a primary identifier.Normalization rules prevent ... 处理的方法为: 菜单栏上的 ...