bzoj 1086 树分块
将树分成一些块,做法见vfleaking博客。
1 /************************************************************** 2 Problem: 1086 3 User: idy002 4 Language: C++ 5 Result: Accepted 6 Time:20 ms 7 Memory:852 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <vector> 12 #define maxn 1010 13 using namespace std; 14 15 16 int n, b; 17 int mccno[maxn]; 18 int cent[maxn], mtot; 19 vector<int> mcc[maxn]; 20 vector<int> g[maxn], remain[maxn]; 21 22 void dfs( int u, int fa ) { 23 for( int t=0; t<g[u].size(); t++ ) { 24 int v = g[u][t]; 25 if( v==fa ) continue; 26 dfs(v,u); 27 while( remain[v].size() ) { 28 remain[u].push_back( remain[v].back() ); 29 remain[v].pop_back(); 30 } 31 if( remain[u].size() >= b ) { 32 mtot++; 33 cent[mtot] = u; 34 while( !remain[u].empty() ) { 35 mccno[ remain[u].back() ] = mtot; 36 mcc[ mtot ].push_back( remain[u].back() ); 37 remain[u].pop_back(); 38 } 39 } 40 } 41 remain[u].push_back( u ); 42 } 43 44 int main() { 45 scanf( "%d%d", &n, &b ); 46 for( int i=1,u,v; i<n; i++ ) { 47 scanf( "%d%d", &u, &v ); 48 g[u].push_back(v); 49 g[v].push_back(u); 50 } 51 dfs(1,0); 52 while( !remain[1].empty() ) { 53 mccno[ remain[1].back() ] = mtot; 54 mcc[ mtot ].push_back( remain[1].back() ); 55 remain[1].pop_back(); 56 } 57 printf( "%d\n", mtot ); 58 for( int i=1; i<=n; i++ ) 59 printf( "%d ", mccno[i] ); 60 printf( "\n" ); 61 for( int i=1; i<=mtot; i++ ) 62 printf( "%d ", cent[i] ); 63 printf( "\n" ); 64 }