题意:

  “余”人国的国王想重新编制他的 国家。他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理。他的国家有n个城市,编号为1..n。一些城市之间有道路相连,任意两个 不同的城市之间有且仅有一条直接或间接的道路。为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个城市。每个省必须 有一个省会,这个省会可以位于省内,也可以在该省外。但是该省的任意一个城市到达省会所经过的道路上的城市(除了最后一个城市,即该省省会)都必须属于该 省。一个城市可以作为多个省的省会。聪明的你快帮帮这个国王吧!

sol:

   膜拜popoqqq大神犇...题解非常直观清晰...附个链接好了...http://blog.csdn.net/popoqqq/article/details/42772237

code:

  

/**************************************************************
Problem: 1086
User: YCuangWhen
Language: C++
Result: Accepted
Time:12 ms
Memory:3616 kb
****************************************************************/ /*==========================================================================
# Last modified: 2016-02-29 14:01
# Filename: 1086.cpp
# Description:树的分块
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 100000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
int top=0,sume=0;
int n,b,cnt=0;
int to[maxm],belong[maxn],next[maxm],first[maxn];
int s[maxm],root[maxn];
void addedge(int x,int y){
sume++; to[sume]=y; next[sume]=first[x]; first[x]=sume;
}
void dfs(int node,int fa){
int bottom=top;
for (int i=first[node];i!=-1;i=next[i])
if (to[i]!=fa){
dfs(to[i],node);
if (top-bottom>=b){
root[++cnt]=node;
while (top!=bottom) belong[s[top--]]=cnt;
}
}
s[++top]=node;
}
int main(){
read(n); read(b);
FORP(i,1,n) first[i]=-1;
FORP(i,1,n-1) {
int x,y; read(x); read(y);
addedge(x,y); addedge(y,x);
}
dfs(1,0);
while (top) belong[s[top--]]=cnt;
printf("%d\n",cnt);
FORP(i,1,n) printf("%d ",belong[i]); printf("\n");
FORP(i,1,cnt) printf("%d ",root[i]);
}

BZOJ 1086 & 类树的分块的更多相关文章

  1. Bzoj 1086: [SCOI2005]王室联邦(分块)

    1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special Judge Submit: 1557 Solved: 9 ...

  2. bzoj 1086: [SCOI2005]王室联邦 (分块+dfs)

    Description “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个不 ...

  3. [BZOJ 1086] [SCOI2005] 王室联邦 【树分块】

    题目链接:BZOJ - 1086 题目分析 这道题要求给树分块,使得每一块的大小在 [B, 3B] 之间,并且可以通过一个块外的节点(块根)使得整个块联通. 那么我们使用一种 DFS,维护一个栈,DF ...

  4. BZOJ 1086:[SCOI2005]王室联邦(DFS树分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1086 题意:给出n个点的树,让你对树进行分块,每块的大小范围在[b, 3b]之间. 思路:一开始想着 ...

  5. HYSBZ 1086 王室联邦 (树的分块)

    题意:国王想把他的国家划分成若干个省.他的国家有n个城市,是一棵树,即n-1条边,编号为1..n.为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个城市.每个省必须有 ...

  6. BZOJ 1086 王室联邦 | BFS

    BZOJ 1086 王室联邦 题意 把一棵树分块,每块大小在[B, 3B]之间(B由输入数据给出),每个块需要对应一个核心点,核心点可以在块内,这个点要满足块内每个点到核心点的路径上的点都属于这个块( ...

  7. BZOJ 3343: 教主的魔法(分块+二分查找)

    BZOJ 3343: 教主的魔法(分块+二分查找) 3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1172  Solved:  ...

  8. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. BZOJ 2243 染色 | 树链剖分模板题进阶版

    BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...

随机推荐

  1. hdu 1279 验证角谷猜想

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1279 #include<stdlib.h> #include<time.h> ...

  2. 谈谈我的编程之路---WAMP(一)

    WAMP的一些配置与使用心得(PHP) 记得第一次接触PHP的时候,我都不知道PHP为什么要大写,但是我却用它来进行工作了,有时候生活就是一场美丽的邂逅 青涩的我,在ES哥的引领下,第一次接触到了WA ...

  3. Sample Apps by Android Team -- Amazed

    Sample Apps by Android Team 代码下载:http://pan.baidu.com/s/1eSNmdUE , 代码原地址:https://code.google.com/arc ...

  4. mysql 5.6.33 重置密码后报错

    [root@ Desktop]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; o ...

  5. 重温WCF之WCF中可靠性会话(十四)

    1.WCF中可靠性会话在绑定层保证消息只会被传输一次,并且保证消息之间的顺序.当使用TCP(Transmission Control Protocol,传输控制协议)通信时,协议本身保证了可靠性.然而 ...

  6. android 入门-动画与容器

    set 动画容器 可作为资源id添加R.anim.xxxx   可用于在样式表中添加  http://blog.csdn.net/liuhe688/article/details/6660823 in ...

  7. AngularJS - 指令入门

    指令,我将其理解为AngularJS操作HTML element的一种途径. 由于学习AngularJS的第一步就是写内置指令ng-app以指出该节点是应用的根节点,所以指令早已不陌生. 这篇日志简单 ...

  8. html5 head头标签

    桌面端开发中,meta标签通常用来为搜索引擎优化(SEO)及 robots定义页面主题,或者是定义用户浏览器上的cookie:它可以用于鉴别作者,设定页面格式,标注内容提要和关键字:还可以设置页面使其 ...

  9. 设置type为file的input标签选择图片类型

    设置能选择各种类型的图片如:png,jpg <input id="file" name="file" type="file" acce ...

  10. RecyclerView 介绍 01

    RecyclerView是Android support v7里面是一个自定义控件.用来显示大量数据集合.类似ListView和GridView这两个控件,RecyclerView同样可以实现,甚至更 ...