• bzoj1086 [SCOI2005]王室联邦


    Description

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

    Input

      第一行包含两个数N,B(1<=N<=1000, 1 <= B <= N)。接下来N-1行,每行描述一条边,包含两个数,即这条边连接的两个城市的编号。

    Output

      如果无法满足国王的要求,输出0。否则输出数K,表示你给出的划分方案中省的个数,编号为1..K。第二行输出N个数,第I个数表示编号为I的城市属于的省的编号,第三行输出K个数,表示这K个省的省会的城市编号,如果有多种方案,你可以输出任意一种。

    Sample Input

    8 2
    1 2
    2 3
    1 8
    8 7
    8 6
    4 6
    6 5

    Sample Output

    3
    2 1 1 3 3 3 3 2
    2 1 8

    正解:块状树。

    神犇讲得很好:http://www.cnblogs.com/shenben/p/6368457.html

    树分块分为dfs序分块,树高分块,子树分块3种,这道题因为有大小限制,所以不能用这3种方法分块。

    不过我们只要统计子树中有多少个点不属于任何一个块,如果>=B个,那么直接把这些点设为一个块,省会就是这个子树的根。最后还剩一些点没有分到块里,直接把这些点放到最后一个块里就行了,这个块的大小肯定不会超过3B。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <cstdio>
     7 #include <vector>
     8 #include <cmath>
     9 #include <queue>
    10 #include <stack>
    11 #include <map>
    12 #include <set>
    13 #define inf (1<<30)
    14 #define N (1010)
    15 #define il inline
    16 #define RG register
    17 #define ll long long
    18 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    19 
    20 using namespace std;
    21 
    22 struct edge{ int nt,to; }g[2*N];
    23 
    24 int head[N],rt[N],bl[N],st[N],n,b,cnt,top,num;
    25 
    26 il int gi(){
    27     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    28     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
    29 }
    30 
    31 il void insert(RG int from,RG int to){ g[++num]=(edge){head[from],to},head[from]=num; return; }
    32 
    33 il void dfs(RG int x,RG int p){
    34     RG int Bl=top,v;
    35     for (RG int i=head[x];i;i=g[i].nt){
    36     v=g[i].to; if (v==p) continue; dfs(v,x);
    37     if (top-Bl>=b){
    38         rt[++cnt]=x;
    39         for (;top!=Bl;--top) bl[st[top]]=cnt;
    40     }
    41     }
    42     st[++top]=x; return;
    43 }
    44 
    45 il void work(){
    46     n=gi(),b=gi(); if (n<b){ puts("0"); } RG int u,v;
    47     for (RG int i=1;i<n;++i) u=gi(),v=gi(),insert(u,v),insert(v,u);
    48     dfs(1,0); if (!cnt) cnt++,rt[cnt]=1; for (;top;--top) bl[st[top]]=cnt;
    49     printf("%d
    ",cnt); for (RG int i=1;i<=n;++i) printf("%d ",bl[i]); puts("");
    50     for (RG int i=1;i<=cnt;++i) printf("%d ",rt[i]); puts(""); return;
    51 }
    52 
    53 int main(){
    54     File("kingdom");
    55     work();
    56     return 0;
    57 }
  • 相关阅读:
    linux磁盘挂载
    3个方法解决百度网盘限速 (2018-07-20)
    mysql状态分析之show global status
    Cgroups子系统介绍
    Go语言 关键字:defer
    Go语言 map的实现
    Go语言 基本类型
    MySQL 监控指标
    sshpass的使用方法
    C++11 std::ref使用场景
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6582329.html
Copyright © 2020-2023  润新知