BZOJ 1086 [SCOI2005]王室联邦 (树上分块)

1086: [SCOI2005]王室联邦

Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special Judge
Submit: 2180  Solved: 1337
[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

析:就是树上分块,这个就可以按莫队的方法来分块。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-4;
const int maxn = 1000 + 10;
const int maxm = 1e6 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

int SIZE;
struct Edge{
  int to, next;
};
Edge edges[maxn<<1];
int head[maxn], cnt;
int pos[maxn], st[maxn];
int top, ans[maxn];

void addEdge(int u, int v){
  edges[cnt].to = v;
  edges[cnt].next = head[u];
  head[u] = cnt++;
}

void dfs(int u, int fa){
  int bot = top;
  for(int i = head[u]; ~i; i = edges[i].next){
    int v = edges[i].to;
    if(v == fa)  continue;
    dfs(v, u);
    if(top - bot >= SIZE){
      ans[++m] = st[top];
      while(top != bot)  pos[st[top--]] = m;
    }
  }
  st[++top] = u;
}

int main(){
  scanf("%d %d", &n, &SIZE);  ms(head, -1);
  for(int i = 1; i < n; ++i){
    int u, v;  scanf("%d %d", &u, &v);
    addEdge(u, v);
    addEdge(v, u);
  }
  dfs(1, -1);
  while(top)  pos[st[top--]] = m;
  printf("%d
", m);
  for(int i = 1; i <= n; ++i)  printf("%d%c", pos[i], i == n ? '
' : ' ');
  for(int i = 1; i <= m; ++i)  printf("%d%c", ans[i], i == m ? '
' : ' ');
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7868436.html