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发行版本系列 ...
随机推荐
- linux install JDK
安装JDK 下载jdk-6u23-linux-i586.bin,samba,FTP cd /usr/local/src/ wget http://www.aminglinux.com/bbs/data ...
- [HDU5324]Boring Class
vjudge sol 字典序最小可以通过倒着\(dp\)解决.对每个\(i\)记录它可以转移到的\(dp\)值最大且字典序最小的\(nxt_i\). 尝试着写一下\(dp\)式子. \[dp_i=ma ...
- Raid信息丢失数据恢复及oracle数据库恢复验证方案
早些时候,有个客户14块盘的磁盘阵列出现故障,需要恢复的数据是oracle数据库,客户在寻求数据恢复技术支持,要求我提供详细的数据恢复方案,以下是提供给客户的详细数据恢复解决方案,本方案包含Raid数 ...
- hdu 3932 Groundhog Build Home——模拟退火
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 注意平均值与最远的点距离为0的情况.所以初值设成-1,这样 id 就不会乱.不过设成0也可以.注意判 ...
- flask+blueprint路由配置
1.flask默认的静态文件和html文件在app应用文件夹里的相应文件夹下:app // Flask||--static ||--templates |静态文件默认的url地址为:url_prefi ...
- 根据VM的TAG开停机
公有云一个非常大的优势,就是可以根据需求进行开停机.由于计费是按时进行的,从而实现节省成本. Azure上用脚本按时开停机已经有很多部署的案例.本文将介绍采用VM Tag中规定的时间进行开停机的脚本. ...
- 蓝桥杯 算法训练 ALGO-156 表达式计算
算法训练 表达式计算 时间限制:1.0s 内存限制:256.0MB 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输 ...
- Hot resize Multipath Disk – Linux
This post is for the users of the great dm-multipath system in Linux, who encounter a major availabi ...
- wpf数据验证实例及常用方法小结
虽然标题是wpf数据验证,但并不是对IDataErrorInfo.ValidationRule.属性中throw Exception这几种验证方式的介绍: 之前做项目时(例如员工工资管理),根据员工编 ...
- VMware:Configuration file was created by a VMware product with more features than this version
Few days ago,I opened the Genesys demo VM by VMware Server 1.0.4 and got an error like this: "C ...