zoukankan      html  css  js  c++  java
  • 【BZOJ-1086】王室联邦 分块 + 块状树

    1086: [SCOI2005]王室联邦

    Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special Judge
    Submit: 1094  Solved: 650
    [Submit][Status][Discuss]

    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

    HINT

    Source

    Solution

    对树分块..方法有点高端

    还是直接转正解吧:PoPoQQQ  VFleaKing

    大体上就是对树DFS,用一个栈去记录,维护一个栈底,balabala

    Code 

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int read()
    {
        int x=0,f=1; char ch=getchar();
        while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
        while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
        return x*f;
    }
    #define maxn 1010
    struct Edgenode{int next,to;}edge[maxn<<1];
    int head[maxn],cnt=1;
    void add(int u,int v) {cnt++;edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt;}
    void insert(int u,int v) {add(u,v);add(v,u);}
    int stack[maxn<<2],top=1,tot,N,B,rt[maxn],root[maxn];
    void DFS(int now,int last)
    {
        int down=top;
        for (int i=head[now]; i; i=edge[i].next)
            if (edge[i].to!=last)
                {
                    DFS(edge[i].to,now);
                    if (top-down>=B) 
                        {
                            root[++tot]=now;
                            while (top>down) rt[stack[top--]]=tot;
                        }
                }
        stack[++top]=now;
    //    printf("%d  %d
    ",now,last);
    }
    int main()
    {
        N=read(),B=read();
        for (int u,v,i=1; i<=N-1; i++)  u=read(),v=read(),insert(u,v);
        DFS(1,0);
        while (top>1) rt[stack[top--]]=tot;
        printf("%d
    ",tot);
        for (int i=1; i<=N; i++) printf("%d ",rt[i]); 
        puts("");
        for (int i=1; i<=tot; i++) printf("%d ",root[i]);
        return 0;
    }
  • 相关阅读:
    初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化
    在ASP.NET MVC控制器中获取链接中的路由数据
    在ASP.NET MVC中对手机号码的验证
    在ASP.NET MVC中实现登录后回到原先的界面
    使用jQuery实现input数值的增量和减量
    ZooKeeper系列(1):安装搭建ZooKeeper环境
    ZooKeeper系列(3):znode说明和znode状态
    ZooKeeper系列(2):ZooKeeper命令行工具zkCli.sh
    翻译:ZooKeeper OverView
    MySQL高可用之组复制(4):详细分析组复制理论
  • 原文地址:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5432597.html
Copyright © 2011-2022 走看看