# BZOJ 1086 王室联邦

## 题解

VFK的糖果公园题解让我先来把这道题做了……

``````#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template <class T>
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
#define space putchar(' ')
#define enter putchar('\n')

const int N = 1005;
int n, B, stk[N], top, bel[N], cap[N], idx;

go[++ecnt] = v;
}
void dfs(int u, int pre){
int st = top;
for(int e = adj[u], v; e; e = nxt[e])
if(v = go[e], v != pre){
dfs(v, u);
if(top - st >= B){
cap[++idx] = u;
while(top > st) bel[stk[top--]] = idx;
}
}
stk[++top] = u;
}

int main(){

for(int i = 1, u, v; i < n; i++)
dfs(1, 0);
while(top) bel[stk[top--]] = idx;
write(idx), enter;
for(int i = 1; i <= n; i++)
write(bel[i]), i == n ? enter : space;
for(int i = 1; i <= idx; i++)
write(cap[i]), i == idx ? enter : space;

return 0;
}``````
原文作者：胡小兔
原文地址: https://www.cnblogs.com/RabbitHu/p/BZOJ1086.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。