#include #include #include #include #include #include using namespace std; const int INF = 1e8; int main() { int n, x, y; cin >> n; vector< vector > next(n+1, vector()); for (int i = 1; i < n; i++) { cin >> x >> y; next[x].push_back(y); next[y].push_back(x); } queue > que; pair p; vector dist(n+1, INF); for (int i = 1; i <= n; i++) { if (i == 1 || next[i].size() == 1) { dist[i] = 0; que.push(make_pair(i, 0)); } } int z; while (!que.empty()) { p = que.front(); que.pop(); for (int i = 0; i < next[p.first].size(); i++) { z = next[p.first][i]; if (p.second + 1 < dist[z]) { dist[z] = p.second + 1; que.push(make_pair(z, p.second + 1)); } } } for (int i = 1; i <= n; i++) { cout << dist[i] << endl; } return 0; }