http://www.lydsy.com/JudgeOnline/problem.php?id=1086
Test instructions: N-point tree, requiring chunking, so that each block is in [B, 3b] and the block formed by a point is connected (a point can be either inside or outside the block) (n<=1000, B<=n)
#include <bits/stdc++.h>using namespace Std;const int n=1005;int ihead[n], CNT, s[n], top, p[n], root[n], num, B, N; struct E {int next, to;} e[n<<1];void Add (int x, int y) {e[++cnt]= (e) {ihead[x], y}; ihead[x]=cnt; e[++cnt]= (e) {ihead[y], x}; ihead[y]=cnt;} void Dfs (int x, int f) {int last=top;for (int i=ihead[x]; i; i=e[i].next) if (e[i].to!=f) {DFS (e[i].to, x); if (top-last>=b {++num; root[num]=x; while (Top!=last) P[s[top--]]=num;}} S[++top]=x;} int main () {scanf ("%d%d", &n, &b), for (int i=0; i<n-1; ++i) {int x, y; scanf ("%d%d", &x, &y); Add (x, y) ; }dfs ((n+1) >>1, 0), while (top) p[s[top--]]=num;printf ("%d\n", num), and for (int i=1; i<=n; ++i) printf ("%d", p[i]); Puts (""); for (int i=1; i<=num; ++i) printf ("%d", Root[i]); return 0;}
Block Tree Series = = First teach you how to divide the tree = =
The only difference is that the problem is not that you really have a block tree that makes each block interconnected, but adds an additional point to make the block connect.
Worship the VFK and popoqqq god Ben of the puzzle after self-yy, found in fact is this = =:
First we can use a strategy: for the current point x, a tree traverses the subtree. The subtree returns me a "connected block" connected to me with a number less than B. If I get the connected block size >=b (<=2b-2 at this point), merge these connected blocks into one block, and "a point" is the current point. At last I returned to my father a "connected block" of less than B (which of course now adds itself so the whole block is connected). Of course you will ask, the last root to get the connected block if <b how to do! Does not meet test instructions Ah! Mom, think about it. = = Just find a point to merge on it. = = because all of the block sizes you've merged before are <=2b-2. = = This is the origin of the title 3b =
For maintaining so-called "connected blocks", use a segmented stack = =
"Bzoj" 1086: [SCOI2005] Royal Confederation