1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 200006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m; vi G[MAXN];
int siz[MAXN] , idx[MAXN]; int ps; void dfs( int u , int f ) { siz[u] = 1; int mx = 0; for( int v : G[u] ) if( v != f ) dfs( v , u ) , siz[u] += siz[v] , mx = max( mx , siz[v] ); mx = max( mx , n - siz[u] ); if( mx <= n / 2 ) ps = u; }
int g[MAXN][20] , dep[MAXN]; void afs( int u , int f ) { dep[u] = dep[f] + 1; siz[u] = 1; for( int v : G[u] ) if( v != f ) { g[v][0] = u; rep( k , 1 , 18 ) if( g[g[v][k-1]][k-1] ) g[v][k] = g[g[v][k-1]][k-1]; else break; afs( v , u ); siz[u] += siz[v]; } }
int lca( int u , int v ) { if( dep[u] < dep[v] ) swap( u , v ); if( dep[u] != dep[v] ) for( int k = 18 ; k >= 0 ; -- k ) if( dep[g[u][k]] >= dep[v] ) u = g[u][k]; if( u == v ) return u; for( int k = 18 ; k >= 0 ; -- k ) if( g[u][k] != g[v][k] ) u = g[u][k] , v = g[v][k]; return g[u][0]; }
int dis( int a , int b ) { return dep[a] + dep[b] - 2 * dep[lca( a , b )] + 1; }
tuple<int,int,int> cur; int as[MAXN];
void solve() { cin >> n; rep( i , 2 , n ) { int u , v; scanf("%d%d",&u,&v); G[u].pb( v ) , G[v].pb( u ); } dfs( 1 , 1 ) , afs( ps , ps ); rep( i , 1 , n ) idx[i] = i; sort( idx + 1 , idx + 1 + n , [&]( int a , int b ) { return siz[a] > siz[b]; } ); cur = make_tuple( 1 , ps , ps ); rep( i , 2 , n ) { int u = idx[i] , s = siz[idx[i]] , a = get<1>( cur ) , b = get<2>( cur ); cur = max( { cur , make_tuple( dis( u , a ) , u , a ) , make_tuple( dis( u , b ) , u , b ) } ); as[s] = get<0>( cur ); } per( i , n , 1 ) as[i] = max( as[i + 1] , as[i] ); rep( i , 1 , n ) { printf("%d\n",( i & 1 ) ? 1 : as[i / 2] ); } }
signed main() {
solve(); }
|