#include using namespace std; #define iter(c) __typeof((c).begin()) #define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++) #define REP(i, a, b) for (int i = a; i < (int)(b); i++) #define rep(i, n) REP(i, 0, n) #define mp make_pair typedef vector vi; typedef pair pii; const int INF = 1 << 29; template bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } struct prepare { prepare() { cout.setf(ios::fixed, ios::floatfield); cout.precision(8); ios_base::sync_with_stdio(false); } } _prepare; const int MAX_N = (int) (1e5) + 1; vi edges[MAX_N]; int R[MAX_N]; int L[MAX_N]; int main() { int N; scanf("%d", &N); rep(i, N - 1) { int x, y; scanf("%d%d", &x, &y); --x, --y; edges[x].push_back(y); edges[y].push_back(x); } memset(R, -1, sizeof(R)); fill(L, L + MAX_N, INF); queue que, leaf_que; que.push(mp(0, 0)); while (!que.empty()) { pii qp = que.front(); que.pop(); int v = qp.first; int d = qp.second; R[v] = d; bool has_child = false; tr(it, edges[v]) { if (R[*it] == -1) { que.push(mp(*it, d + 1)); has_child = true; } } if (!has_child) leaf_que.push(mp(v, 0)); } while (!leaf_que.empty()) { pii qp = leaf_que.front(); leaf_que.pop(); const int v = qp.first; const int d = qp.second; if (!chmin(L[v], d)) continue; tr(it, edges[v]) { if (L[*it] > d + 1) { leaf_que.push(mp(*it, d + 1)); } } } rep(i, N) printf("%d\n", min(R[i], L[i])); return 0; }