## https://yukicoder.me/problems/no/1418 from collections import deque def main(): N = int(input()) next_nodes = [[] for _ in range(N)] for _ in range(N - 1): a, b = map(int, input().split()) next_nodes[a - 1].append(b - 1) next_nodes[b - 1].append(a - 1) # 全方位木dp parents =[-2] * N parents[0] = -1 total_child_num = [0] * N next_childs = [{} for _ in range(N)] stack = deque() stack.append((0, 0)) while len(stack ) > 0: v, index = stack.pop() while index < len(next_nodes[v]): w = next_nodes[v][index] if w == parents[v]: index += 1 continue parents[w] = v stack.append((v, index + 1)) stack.append((w, 0)) break if index == len(next_nodes[v]): p = parents[v] if p != -1: total_child_num[p] += 1 + total_child_num[v] next_childs[p][v] = 1 + total_child_num[v] queue = deque() queue.append(0) while len(queue) > 0: v = queue.popleft() for w in next_nodes[v]: if parents[v] == w: continue n = N - next_childs[v][w] next_childs[w][v] = n queue.append(w) answer = 0 for v in range(N): answer += N for value in next_childs[v].values(): c = N - value ans = value * c answer += ans print(v, ans) print(answer) if __name__ == "__main__": main()