#include #include #include using namespace std; struct treenode { int num; int depth; int parent; treenode(int num, int depth, int parent) : num(num), depth(depth), parent(parent) {} }; int N, x, y; vector> rinsetsu; vector leaves; int ans[100001] = {}; void dfs(int start, bool happa) { stack st; st.emplace(start, 0, -1); while (!st.empty()) { treenode n = st.top(); st.pop(); if (ans[n.num] > n.depth) ans[n.num] = n.depth; if (happa && (int)rinsetsu[n.num].size() == 1 && n.parent > -1) { leaves.push_back(n.num); continue; } for (auto &nn:rinsetsu[n.num]) { if (nn != n.parent) st.emplace(nn, n.depth+1, n.num); } } } int main() { cin >> N; rinsetsu = vector>(N); for (int i = 0; i < N-1; i++) { cin >> x >> y; rinsetsu[x-1].push_back(y-1); rinsetsu[y-1].push_back(x-1); } for (int i = 0; i < N; i++) { ans[i] = 200000; } dfs(0, true); for (auto &leaf:leaves) dfs(leaf, false); for (int i = 0; i < N; i++) cout << ans[i] << endl; return 0; }