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发行版本系列 ...
随机推荐
- LeetCode OJ:Count and Say(数数)
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- Hadoop单机模式和伪分布式搭建教程CentOS
1. 安装JAVA环境 2. Hadoop下载地址: http://archive.apache.org/dist/hadoop/core/ tar -zxvf hadoop-2.6.0.tar.gz ...
- 剑指offer--15.把字符串转换成整数
stringstream做这个真的很舒服 ------------------------------------------------------------------------------- ...
- POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)
题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...
- 排列(加了点小set就过了,哈哈哈)
Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. 输入描述: 1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0输出 ...
- 学习动态性能表(21)v$lincense
学习动态性能表 第20篇--v$lincense 2007.6.15 本节主要参数: SESSION_MAX:实例允许的并发最大session数量 SESSION_WARNING:当前实例警告的并发 ...
- 学习动态性能表(8)--v$lock&v$locked_object
学习动态性能表 第八篇-(1)-V$LOCK 2007.5.31 这个视图列出Oracle 服务器当前拥有的锁以及未完成的锁或栓锁请求.如果你觉着session在等待等待事件队列那你应该检查本视图. ...
- hive JDBC异常到多租户
hive jdbc执行select count(*) from test报错. return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedT ...
- phpstorm下载和破解
http://idea.qinxi1992.cn/ 官网下载对应版本,在下面进行破解! storm官网:https://www.jetbrains.com/phpstorm/ 破解网址 :http: ...
- Ext.window.Window
var win = Ext.create("Ext.window.Window", { id: "myWin", title: "示例窗口" ...