100198H Royal Federation
题目大意
国家有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的更多相关文章
- acdream1233 Royal Federation (构造?)
http://acdream.info/problem?pid=1233 Andrew Stankevich's Contest (3) ASC 3 Royal Federation Special ...
- HDFS Federation (读书笔记)
HDFS Federation (读书笔记) HDFS的架构 HDFS包含两个层次:命名空间管理(Namespace) 和 块/存储管理(Block Storage). 命名空间管理(Namespac ...
- 国内第一篇详细讲解hadoop2的automatic HA+Federation+Yarn配置的教程
前言 hadoop是分布式系统,运行在linux之上,配置起来相对复杂.对于hadoop1,很多同学就因为不能搭建正确的运行环境,导致学习兴趣锐减.不过,我有免费的学习视频下载,请点击这里. hado ...
- [zz]The Royal Treatment
http://www.cgw.com/Publications/CGW/2012/Volume-35-Issue-4-June-July-2012/The-Royal-Treatment.aspx T ...
- hadoop2的automatic HA+Federation+Yarn配置的教程
前言 hadoop是分布式系统,运行在linux之上,配置起来相对复杂.对于hadoop1,很多同学就因为不能搭建正确的运行环境,导致学习兴趣锐减.不过,我有免费的学习视频下载,请点击这里. hado ...
- 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 ...
- 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 ...
- Hadoop 学习笔记 (十) hadoop2.2.0 生产环境部署 HDFS HA Federation 含Yarn部署
其他的配置跟HDFS-HA部署方式完全一样.但JournalNOde的配置不一样>hadoop-cluster1中的nn1和nn2和hadoop-cluster2中的nn3和nn4可以公用同样的 ...
- HDFS Federation与HDFS High Availability详解
HDFS Federation NameNode在内存中保存文件系统中每个文件和每个数据块的引用关系,这意味着对于一个拥有大量文件的超大集群来说,内存将成为限制系统横向扩展的瓶颈.在2.0发行版本系列 ...
随机推荐
- bzoj 4710 分特产
有 $n$ 个人,$m$ 种物品,每种物品有 $a_i$ 个,求每个人至少分到一个的方案数 $n,m,a_i \leq 2000$ sol: 比上一个题简单一点 还是考虑容斥 每个人至少分到一个 = ...
- BZOJ4355: Play with sequence
BZOJ4355: Play with sequence https://lydsy.com/JudgeOnline/problem.php?id=4355 分析: 模板题. 把\(2\)操作看成先区 ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- Python函数-complex()
complex([real[, imag]]) 作用: 创建一个值为real + imag * j的复数或者转化一个字符串或数为复数.如果第一个参数为字符串,则不需要指定第二个参数. 参数real: ...
- HIVE-分区表详解以及实例
HIVE中的分区表是什么,我们先看操作,然后再来体会. 创建一个分区表,分区的单位时dt和国家名 hive> create table logs(ts bigint,line string) & ...
- 2.Linux下安装Jenkins
1.安装jenkins的前提是安装好jdk环境,自行安装jdk,若安装成功后,使用一下命令即可成功安装jenkins: wget -O /etc/yum.repos.d/jenkins.repo ht ...
- 证书脚本--生成csr,key
#!/bin/sh # this script can make certificate of each line in file you point which one! if [ $# -ne 1 ...
- Docker容器的重启策略及docker run的--restart选项详解
https://blog.csdn.net/taiyangdao/article/details/73076019 1. Docker容器的重启策略 Docker容器的重启策略是面向生产环境的一个启动 ...
- Day1--Python基础1--下半部分
一..pyc是什么 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存在 ...
- Spring-@value用法详解
为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性. 一.两种使用方法 1.@Value("#{co ...