This page is still under construction.

Parts of this page are still being built. What you see may change.

Bus Routes

Interview

Time limit3sMemory limit1024 MB

Summary
On a tree with N stops, every ordered pair sends a bus along the unique path; for each stop count how many of the N(N-1) buses halt there, including endpoints.
Level

Medium5 of 10

Topics
Tree, Math, DFS
Solved
No attempts yet

Problem

A Czech town called Kocourkov has a public transport system. It has NN bus stops and N−1N - 1 two-way roads, and each road joins two different stops. From any stop you reach every other stop by following roads.

Every morning each stop sends out exactly one bus to every other stop, so there are N(N−1)N(N - 1) buses in total. A bus halts once at every stop on the route from its origin to its destination.

Every stop needs a timetable that lists all the buses halting there. The buses that begin their route at that stop and the buses that end their route there belong on the timetable too.

You are given a description of the transport system in Kocourkov. For every stop, count the buses that halt there.

Input

The first line contains NN, the number of bus stops. The stops are numbered 1 through NN. Each of the next N−1N - 1 lines describes one road of the town and contains the numbers of the two different stops it joins, xx and yy (1≤x,y≤N1 \le x, y \le N).

1≤N≤1061 \le N \le 10^6.

Output

Print NN lines. Line ii contains a single integer, the number of buses that halt at stop ii.

Examples3

  1. Example 1

    Input
    6
    1 2
    2 3
    3 4
    4 5
    5 6
    
    Expected output
    10
    18
    22
    22
    18
    10
    
  2. Example 2

    Input
    5
    4 5
    2 1
    3 2
    2 5
    
    Expected output
    8
    18
    8
    8
    14
    
  3. Example 3

    Input
    12
    8 6
    1 10
    10 6
    5 9
    11 6
    7 5
    6 5
    12 10
    8 3
    4 2
    4 10
    
    Expected output
    22
    22
    22
    42
    60
    104
    22
    42
    22
    88
    22
    22