#include #include #include "testlib.h" using std::cout; using std::vector; constexpr int MAX_N = 100000; struct Solver { Solver(int n, vector>& g) : N(n), graph(g), memo(n) {} int N; vector> graph; vector memo; int dfs(int num, int difference, int parent = -1) { int result = 0; memo[num] = difference; for (int next : graph[num]) { if (next == parent) continue; if (num > next) ++result; result += dfs(next, difference + (num > next ? -1 : 1), num); } return result; } vector solve() { int first_level = dfs(0, 0); vector result(N); for (int i = 0; i < N; ++i) { result[i] = first_level + memo[i]; } return result; } }; int main() { registerValidation(); int N = inf.readInt(2, MAX_N, "N"); inf.readEoln(); vector graph(N, vector()); for (int i = 0; i < N - 1; ++i) { int A = inf.readInt(1, N, "A_i"); inf.readSpace(); int B = inf.readInt(1, N, "B_i"); inf.readEoln(); --A; --B; graph[A].push_back(B); graph[B].push_back(A); } inf.readEof(); Solver solver(N, graph); for (int answer : solver.solve()) { cout << answer << "\n"; } }