#include using namespace std; using ll = long long; using PII = std::pair; using PLL = std::pair; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) vector depth, height, parent; vector> nodes; void dfs1(int s, int d){ depth[s] = d; for (auto z : nodes[s]) if (z != parent[s]) { parent[z] = s; dfs1(z, d+1); } } void bfs(vector leaf, int n){ int turn = 0; vector search, new_search; search = leaf; while (!search.empty()) { for (auto i : search) { height[i] = turn; for (auto j : nodes[i]) { if (height[j] == n){ new_search.push_back(j); height[j] = turn + 1; } } } search.clear(); search = new_search; new_search.clear(); turn++; } } int main() { #ifdef DEBUG cout << "DEBUG MODE" << endl; ifstream in("input.txt"); //for debug cin.rdbuf(in.rdbuf()); //for debug #endif int n, a, b; cin >> n; vector leaf; depth.resize(n), height.resize(n), parent.resize(n), nodes.resize(n); parent[0] = -1; rep(i, n-1) { cin >> a >> b; a--, b--; nodes[a].push_back(b); nodes[b].push_back(a); } dfs1(0, 0); fill(height.begin(), height.end(), n); rep(i, n) if (nodes[i].size() == 1) leaf.push_back(i); bfs(leaf, n); // rep(i, n) // cout << height[i] << endl; rep(i, n) cout << min(depth[i], height[i]) << endl; return 0; }