/********************************************************************* 程序名: 版权: Joecai 作者: Joecai 日期: 2022-04-13 23:47 说明: *********************************************************************/ #include <bits/stdc++.h> using namespace std; #define x first #define y second # define rep(i,be,en) for(int i=be;i<=en;i++) # define pre(i,be,en) for(int i=be;i>=en;i--) #define ll long long #define endl "\n" #define LOCAL #define pb push_back #define int long long typedef pair<ll, ll> PII; #define eb emplace_back #define sp(i) setprecision(i) const int N = 2e5 + 10, INF = 0x3f3f3f3f; ll qpow(int a, int b) { ll ans = 1; while (b) { if (b & 1) ans = ans * a; a = (ll)a * a; b >>= 1; } return ans; } inline int read() { int X = 0; bool flag = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') flag = 0; ch = getchar(); } while (ch >= '0' && ch <= '9') { X = (X << 1) + (X << 3) + ch - '0'; ch = getchar(); } if (flag) return X; return ~(X - 1); } inline void write(int X) { if (X < 0) { X = ~(X - 1); putchar('-'); } if (X > 9) write(X / 10); putchar(X % 10 + '0'); } struct dsu { private: vector<int> par, ran, cnt; public: dsu(int n) { par.resize(n, 0); iota(par.begin(), par.end(), 0); ran.resize(n, 0); cnt.resize(n, 1); } int find(int x) { if (par[x] == x)return x; else return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return false; if (ran[x] < ran[y]) { par[x] = y; cnt[y] += cnt[x]; } else { par[y] = x; cnt[x] += cnt[y]; if (ran[x] == ran[y])ran[x]++; } return true; } bool same(int x, int y) { return find(x) == find(y); } int getcnt(int x) { return cnt[find(x)]; } }; int n; vector<int>g[N]; int sz[N]; int d[N]; int dfs1(int u, int f) { sz[u] = 1; for (auto x : g[u]) { if (x == f) continue; sz[u] += dfs1(x, u); d[u] += d[x] + sz[x]; } return sz[u]; } void dfs(int u, int f) { for (auto x : g[u]) { if (f == x) continue; d[x] = d[u] - 2 * sz[x] + n; dfs(x, u); } } void solve() { cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs1(1, -1); // 求1到每个带点的距离 //cout << d[1] << endl; dfs(1, -1); for (int i = 1; i <= n; i++) { cout << d[i] << endl; } } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //#ifdef LOCAL //freopen("data.in.txt","r",stdin); //freopen("data.out.txt","w",stdout); //#endif int __ = 1; //cin>>__; while (__--) { solve(); } return 0; }