#include using namespace std; typedef long long ll; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef pair< int, int > pii; typedef vector< pii > vp; typedef vector< double > vd; typedef vector< vd > vvd; typedef vector< string > vs; template< class T1, class T2 > int upmin( T1 &x, T2 v ){ if( x > v ){ x = v; return 1; } return 0; } template< class T1, class T2 > int upmax( T1 &x, T2 v ){ if( x < v ){ x = v; return 1; } return 0; } const int INF = 0x3f3f3f3f; int N; vvi G; void init(){ cin >> N; G = vvi( N ); for( int i = 0; i < N - 1; ++i ){ int x, y; cin >> x >> y; G[ x - 1 ].emplace_back( y - 1 ); G[ y - 1 ].emplace_back( x - 1 ); } } vi rtdis, lfdis; void root_dfs( int u, int fa, int dis ){ rtdis[ u ] = dis; for( int v : G[ u ] ){ if( v == fa ) continue; root_dfs( v, u, dis + 1 ); } } void leaf_dfs( int u, int fa ){ lfdis[ u ] = INF; for( int v : G[ u ] ) if( v != fa ) leaf_dfs( v, u ), upmin( lfdis[ u ], lfdis[ v ] + 1 ); if( lfdis[ u ] == INF ) lfdis[ u ] = 0; } void neighbor_leaf_dfs( int u, int fa, int d ){ upmin( lfdis[ u ], d ); int minv = d; for( int v : G[ u ] ) if( v != fa ) upmin( minv, lfdis[ v ] + 1 ); for( int v : G[ u ] ) if( v != fa ) neighbor_leaf_dfs( v, u, minv + 1 ); } void preprocess(){ rtdis = lfdis = vi( N ); root_dfs( 0, -1, 0 ); leaf_dfs( 0, -1 ); neighbor_leaf_dfs( 0, -1, 0 ); } void solve(){ for( int i = 0; i < N; ++i ) cout << min( rtdis[ i ], lfdis[ i ] ) << endl; } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }