import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Queue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); List> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { sa = br.readLine().split(" "); int v = Integer.parseInt(sa[0]) - 1; int w = Integer.parseInt(sa[1]) - 1; list.get(v).add(w); list.get(w).add(v); } br.close(); Obj[] ans = new Obj[n]; List order = new ArrayList<>(); int[] par = new int[n]; Arrays.fill(par, -2); Queue que = new ArrayDeque<>(); que.add(0); order.add(0); par[0] = -1; while (!que.isEmpty()) { int cur = que.poll(); for (int next : list.get(cur)) { if (par[next] == -2) { que.add(next); order.add(next); par[next] = cur; } } } Collections.reverse(order); for (int i : order) { Obj o = new Obj(); long total = 0; for (int c : list.get(i)) { if (c != par[i]) { o.size += ans[c].size; total += ans[c].total; } } o.size++; o.total = (long) o.size * o.size; o.val = o.total - total; ans[i] = o; } PrintWriter pw = new PrintWriter(System.out); for (Obj o : ans) { pw.println(o.val); } pw.flush(); } static class Obj { int size; long val, total; } }