传送门

题目大意

国家有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. OpenCV教程【002 VideoCapture加载并播放视频】

    #include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace c ...

  2. Oracle job procedure 存储过程定时任务(转自hoojo)

    Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 ...

  3. 在.net中读写config文件的各种方法(自定义config节点)

    http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...

  4. linux find -regex 使用正则表达式

    find之强大毋庸置疑,此处只是带领大家一窥find门径,更详细的说明见man  find和 info find.整篇文章循序渐进,从最常用的文件名测试项开始步步深入,到第六节基本讲完find处理文件 ...

  5. Oracle 闪回归档(Flashback Database)

    cmd --管理员身份打开 sqlplus / as sysdba --管理数据库 shu immediate; --独占方式开始 startup mount --修改日期模式 alter datab ...

  6. 蓝桥杯 算法训练 ALGO-119 寂寞的数

    算法训练 寂寞的数 时间限制:1.0s   内存限制:256.0MB 问题描述 道德经曰:一生二,二生三,三生万物. 对于任意正整数n,我们定义d(n)的值为为n加上组成n的各个数字的和.例如,d(2 ...

  7. 一些js和JQuery的方法

    取值填值: js: get-- innerText, innerHTML, value;  set-- innerText=?, innerHTML=?, value=?; JQuery: get-- ...

  8. java代码split分割数字类

    总结:正则表达式-- package com.c2; //写一个spli的用法,数字类 ===分割字符串 public class yqw { public static void main(Stri ...

  9. declare handler 声明异常处理的语法

    声明异常处理的语法 DECLARE {EXIT | CONTINUE} HANDLER FOR {error-number | SQLSTATE error-string | condition} S ...

  10. L2-014. 列车调度(set的使用,最长递增子序列)

    L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...