• 初涉树分块


    树分块:树上莫队基础

    树分块

    丢一个博客:https://blog.csdn.net/chhnz/article/details/70664667

    例题

    1086: [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个省的省会的城市编号,如果
    有多种方案,你可以输出任意一种。


    题目分析

    先把代码扔这

     1 #include<bits/stdc++.h>
     2 const int maxn = 1003;
     3 
     4 int stk[maxn],top;
     5 int rt[maxn],ans[maxn],tot;
     6 int n,b;
     7 std::vector<int> f[maxn];
     8 
     9 void dfs(int x, int fa)
    10 {
    11     int pre = top;
    12     for (int i=0; i<f[x].size(); i++)
    13         if (fa!=f[x][i]){
    14             dfs(f[x][i], x);
    15             if (top-pre >= b){
    16                 rt[++tot] = x;
    17                 while (top!=pre)
    18                     ans[stk[top--]] = tot;
    19             }
    20         }
    21     stk[++top] = x;
    22 }
    23 int main()
    24 {
    25     scanf("%d%d",&n,&b);
    26     for (int i=1; i<n; i++)
    27     {
    28         int x,y;
    29         scanf("%d%d",&x,&y);
    30         f[x].push_back(y), f[y].push_back(x);
    31     }
    32     dfs(1, 0);
    33     while (top) ans[stk[top--]] = tot;
    34     if (!tot) puts("0");
    35     else{
    36         printf("%d
    ",tot);
    37         for (int i=1; i<n; i++) printf("%d ",ans[i]);
    38         printf("%d
    ",ans[n]);
    39         for (int i=1; i<tot; i++) printf("%d ",rt[i]);
    40         printf("%d
    ",rt[tot]);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    TF-IDF
    智力题
    max{X,Y}和min{X,Y}的概率分布
    K-Means
    提高工作效率:给设计师的10条建议
    每个软件工程师都应该尝试的5件事
    CSS sprite 圆角——源代码
    比较大气的网页元素
    彩色网页变黑白色CSS代码变黑白色调!
    jquery适用技巧
  • 原文地址:https://www.cnblogs.com/antiquality/p/9173941.html
Copyright © 2020-2023  润新知