#include<bits/stdc++.h>
using namespace std;
#ifdef local
string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(v[i]);
    }
    res += "}";
    return res;
}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
typedef long long ll;
#define f(i, l, r) for(int i=l;i<=r;i++)
#define rf(i, r, l) for(int i=r;i>=l;--i)
#define all(x) x.begin(),x.end()
const int N = 2e5+10, mod = 1e9+7;
ll power(ll a, ll b) {ll res = 1;while (b) {if (b & 1)res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}
ll INV(ll a){return power(a,mod-2);}
mt19937 Rand(123456);
int Range(int l,int r){
    return l+Rand()%(r-l+1);
}
vector<int> g[N];
int dis[N];
int T,D;
int mx_dep[N],ans[N];
void dfs(int u,int f){
    if(dis[u]>D){
        D=dis[u];T=u;
    }
    mx_dep[u]=dis[u];
    for(int v:g[u])if(v!=f){
        dis[v]=dis[u]+1;
        dfs(v,u);
        mx_dep[u]=max(mx_dep[u],mx_dep[v]);
    }
}
int sz[N];
int step;
void dfs2(int u,int f){
    debug(u,dis[u]);
    sort(all(g[u]),[&](int l,int r){
       return  mx_dep[l]<mx_dep[r];
    });
    ans[u]=++step;
    for(int v:g[u])if(v!=f){
        dfs2(v,u);
    }
    step++;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef local
    freopen("../in.txt", "r", stdin);
#endif
    int n;cin>>n;
    f(i,1,n-1){
        int u,v;cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    D=-1;dfs(1,0);
    D=-1;int k=T;dis[k]=1;dfs(T,0);
    dfs2(k,0);
    f(i,1,n)cout<<ans[i]<<" ";
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int N;
vector<vector<int>> edges;
vector<bool> ondiameter;
vector<int> ans;

pii far(int u, int prev, int d) {
    int node = u, dis = 0;
    for (int v : edges[u]) {
        if (v == prev)
            continue;
        pii temp = far(v, u, d + 1);
        if (dis < temp.second + 1) {
            dis = temp.second + 1;
            node = temp.first;
        }
    }
    return make_pair(node, dis);
}

bool tag(int u, int prev, int target) {
    if (u == target)
        return true;
    for (int v : edges[u]) {
        if (v == prev)
            continue;
        if (tag(v, u, target)) {
            ondiameter[u] = true;
            return true;
        }
    }
    return false;
}

int t = 1;
void solve(int u, int prev) {
    ans[u] = t;
    for (int v : edges[u]) {
        if (v == prev || ondiameter[v])
            continue;
        t++;
        solve(v, u);
    }
    for (int v : edges[u]) {
        if (v != prev && ondiameter[v]) {
            t++;
            solve(v, u);
        }
    }
    t++;
}

int main() {
    cin >> N;
    ondiameter.resize(N + 1);
    edges.resize(N + 1);
    ans.resize(N + 1);
    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    // find diameter
    pii a = far(1, 1, 0);
    pii b = far(a.first, a.first, 0);
    tag(a.first, a.first, b.first);
    solve(a.first, a.first);
    for (int i = 1; i <= N; i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
}