#pragma GCC optimize("Ofast")
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define _roverload(_a1, _a2, _a3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _roverload(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i)
#define rrep(...) _roverload(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
void print() { cout << '\n'; }
template <class T>
void print(const vector<T> &v) {
    bool f = false;
    for (const auto &e : v) {
        if (f) cout << ' ';
        cout << e;
        f = true;
    }
    cout << '\n';
}
template <class... T>
void print(const T &...args) {
    bool f = false;
    for (const auto &e : {args...}) {
        if (f) cout << ' ';
        cout << e;
        f = true;
    }
    cout << '\n';
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
    preprocess() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library
pi dfs(vv<int> &g, int u, int p = -1) {
    int dist = -1, idx = -1;
    for (auto v : g[u]) {
        if (v == p) continue;
        auto [d, i] = dfs(g, v, u);
        if (chmax(dist, d)) {
            idx = i;
        }
    }
    if (dist == -1) {
        return {0, u};
    } else {
        return {dist + 1, idx};
    }
}
vi res;
bool path(vv<int> &g, int u, int u2, int p = -1) {
    if (u == u2) {
        res.emplace_back(u2);
        return true;
    }
    bool r = false;
    for (auto v : g[u]) {
        if (v == p) continue;
        bool f = path(g, v, u2, u);
        r |= f;
        if (f) {
            res.emplace_back(u);
        }
    }
    return r;
}
int now = 1;
void dfs2(vv<int> &to, vi &ans, vi &route, int u, bool flag, int p = -1) {
    ans[u] = now++;
    for (auto v : to[u]) {
        if (v == p) continue;
        if (route.size() && v == route.back()) continue;
        dfs2(to, ans, route, v, false, u);
    }
    if (flag && route.size()) {
        int v = route.back();
        route.pop_back();
        dfs2(to, ans, route, v, true, u);
    }
    now++;
}
int main() {
    int n;
    cin >> n;
    vv<int> to(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to[a].emplace_back(b);
        to[b].emplace_back(a);
    }
    auto [dist, u1] = dfs(to, 0);
    auto [dist2, u2] = dfs(to, u1);
    path(to, u1, u2);
    res.pop_back();
    vi ans(n);
    dfs2(to, ans, res, u1, true);
    print(ans);
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

inline int read () {
	int ret = 0, t = 1;
	char c = getchar();
	while ((c < '0' || c > '9') && c != '-') c = getchar();
	if (c == '-') t = -1, c = getchar();
	while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
	return ret * t;
}

const int MAXN = 200200;
int n, fa[MAXN][22]; vector <int> E[MAXN];
int dep[MAXN], wson[MAXN];
void dfs (int x, int f) {
	dep[x] = dep[f] + 1; fa[x][0] = f;
	for (auto i: E[x]) if (i != f) dfs(i, x);
}

int dis (int u, int v) {
	int ret = dep[u] + dep[v], i;
	if (dep[u] < dep[v]) swap(u, v);
	for (i = 20; i >= 0; i--) if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
	if (u == v) return ret - 2 * dep[u];
	for (i = 20; i >= 0; i--) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
	u = fa[u][0]; return ret - 2 * dep[u];
}

int las = 0, ans[MAXN];
void calc (int x, int f) {
	ans[x] = ans[las] + dis(las, x); las = x;
	for (auto i: E[x]) if (i != f && i != wson[x]) calc(i, x);
	if (wson[x]) calc(wson[x], x);
}

int main () {
	n = read(); int i, j;
	for (i = 1; i < n; i++) {
		int u = read(), v = read();
		E[u].push_back(v), E[v].push_back(u);
	}
	dfs(1, 0);
	for (i = 1, j = 0; i <= n; i++) if (dep[j] < dep[i]) j = i;
	int rt = j; dfs(j, 0);
	for (i = 1, j = 0; i <= n; i++) if (dep[j] < dep[i]) j = i;
	for (i = j; i; i = fa[i][0]) wson[fa[i][0]] = i;
	for (i = 1; i <= 20; i++) for (j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
	calc(rt, 0);
	for (i = 1; i <= n; i++) printf("%d ", ans[i]);
	return 0;
}