• [SCOI2005]王室联邦


    分块基本没有限制
    所以每次大于等于b就分一块

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(2e3 + 10);
    
    IL ll Read(){
        char c = '%'; ll x = 0, z = 1;
        for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
        return x * z;
    }
    
    int b, n, fst[_], num, nxt[_], to[_], cnt, bel[_], S[_], rt[_];
    
    IL void Add(RG int u, RG int v){  to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;  }
    
    IL void Dfs(RG int u, RG int fa){
        RG int x = S[0];
        for(RG int e = fst[u]; e != -1; e = nxt[e]){
            if(to[e] == fa) continue;
            Dfs(to[e], u);
            if(S[0] - x >= b){
                rt[++num] = u;
                while(S[0] != x) bel[S[S[0]--]] = num;
            }
        }
        S[++S[0]] = u;
    }
    
    int main(RG int argc, RG char *argv[]){
        n = Read(); b = Read(); Fill(fst, -1);
        for(RG int i = 1, u, v; i < n; i++) u = Read(), v = Read(), Add(u, v), Add(v, u);
        if(n < b){  puts("0"); return 0;  }
        Dfs(1, 0);
        while(S[0]) bel[S[S[0]--]] = num;
        printf("%d
    ", num);
        for(RG int i = 1; i <= n; i++) printf("%d ", bel[i]);
        printf("
    ");
        for(RG int i = 1; i <= num; i++) printf("%d ", rt[i]);
        printf("
    ");
        return 0;
    }
    
  • 相关阅读:
    【c++】中文设置
    《谁动了我的奶酪》读后感
    KMP算法的C++实现
    我也说说中文分词(上:基于字符串匹配)
    删除字符串中的空格
    linux jdk bin安装
    笔试题汇总
    栈的压入、弹出序列
    顺序打印矩阵
    二叉树镜像
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206343.html
Copyright © 2020-2023  润新知