CSES - Shared codeLink to this code: https://cses.fi/paste/cc3cef8f5f02c90f7c0235/
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define F first
#define S second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

vector<pii> el;

struct ART {
	int ans = 0;
	ART operator+(const ART& otr) const {
		return {max(ans, otr.ans)};
	}
	ART promote(int x, int e) const { // x=node index, e=edge index
		return {ans + 1};
	}
};
template<class T>
struct AllRoots {
	int n;
	vector<vector<pii>> adj;
	vector<vector<T>> pre, suf;
	AllRoots(vector<pii>& el) : n(sz(el)+1), adj(sz(el)+1), pre(sz(el)+1), suf(sz(el)+1) {
		rep(i,0,n-1){
			adj[el[i].F].pb({el[i].S,i});
			adj[el[i].S].pb({el[i].F,i});
		}
		rep(i,0,n) {
			pre[i].resize(adj[i].size() + 1);
			suf[i].resize(adj[i].size() + 1);
		}
	}
	T get(int x, int i = 0){
		T& v = suf[x][i+1];
		if(i) v = pre[x][i] + v; 
		return v.promote(x, adj[x][i].S);
	};
	T down(int x, int p = -1) {
		for(auto& c : adj[x]) if(c.F == p) swap(c, adj[x][0]);
		for(int i = adj[x].size()-1; i > -(p<0); --i)
			suf[x][i] = down(adj[x][i].F, x) + suf[x][i+1];
		return get(x);
	}
	void up(int x, int p = -1) {
		rep(i, p != -1, adj[x].size()) {
			pre[x][i+1] = pre[x][i] + get(adj[x][i].F);
			pre[adj[x][i].F][1] = get(x, i);
			up(adj[x][i].F, x);
		}
	}
	static vector<T> calc(vector<pii>& el) {
		AllRoots ar(el);
		ar.down(0);
		ar.up(0);
		vector<T> res(ar.n);
		rep(i,0,ar.n) res[i] = ar.pre[i].back(); // optional promote here.
		return res;
	}
};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	int n;cin>>n;
	el.resize(n-1);
	rep(i,0,n-1){
		cin>>el[i].F>>el[i].S;
		el[i].F--; el[i].S--;
	}
	auto res = AllRoots<ART>::calc(el);
	rep(i,0,n) cout << res[i].ans << ' ';
	cout<<'\n';
}