Problem:
Score : 1200 points

Problem Statement

Takahashi's office has N rooms. Each room has an ID from 1 to N.
There are also N-1 corridors, and the i-th corridor connects Room a_i and Room b_i.
It is known that we can travel between any two rooms using only these corridors.

Takahashi has got lost in one of the rooms. Let this room be r.
He decides to get back to his room, Room 1, by repeatedly traveling in the following manner:

Travel to the room with the smallest ID among the rooms that are adjacent to the rooms already visited, but not visited yet.

Let c_r be the number of travels required to get back to Room 1.
Find all of c_2,c_3,...,c_N.
Note that, no matter how many corridors he passes through in a travel, it still counts as one travel.

Constraints

2 \leq N \leq 2\times 10^5

1 \leq a_i,b_i \leq N

a_i \neq b_i

The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print c_r for each r, in the following format:

c_2 c_3 ... c_N

Sample Input 1

6
1 5
5 6
6 2
6 3
6 4

Sample Output 1

5 5 5 1 5

For example, if Takahashi was lost in Room 2, he will travel as follows:

Travel to Room 6.

Travel to Room 3.

Travel to Room 4.

Travel to Room 5.

Travel to Room 1.

Sample Input 2

6
1 2
2 3
3 4
4 5
5 6

Sample Output 2

1 2 3 4 5

Sample Input 3

10
1 5
5 6
6 10
6 4
10 3
10 8
8 2
4 7
4 9

Sample Output 3

7 5 3 1 3 4 7 4 5

Solution in C (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>

#define MAX_N (200000)

int qsort_compare_int_x2(const void *a, const void *b)
{
    int *a_int, *b_int;
    a_int = (int *) a;
    b_int = (int *) b;
    if (a_int[0] > b_int[0]) {return  1;}
    if (a_int[0] < b_int[0]) {return -1;}
    if (a_int[1] > b_int[1]) {return  1;}
    if (a_int[1] < b_int[1]) {return -1;}
    return 0;
}

int move(int room, int n, int a[MAX_N * 2][2], int a_marker[MAX_N])
{
    int i;
    int count;
    int now;
    int next;
    int rooms = (n / 2) + 1;
    int min;

    char b[MAX_N + 1]; // 1 = 訪れた, 2 = 行ける(隣接)

    for (i = 1; i <= rooms; i++) {
        b[i] = 0;
    }

    now = room;

    count = 0;
    while(1) {
        b[now] = 1;
        for (i = a_marker[now]; a[i][0] == now; i++) {
            // 今いる部屋との接続
            next = a[i][1];
            if (b[next] != 1) {b[next] = 2;}
        }
        for (i = 1; i <= rooms; i++) {
            if (b[i] == 2) {
                if (i == 1) {return count + 1;}
                now = i;
                count++;
                break;
            }
        }
    }
}

int main(int argc, char *argv[])
{
    int n, i, j;
    int a[MAX_N * 2][2];
    int a_marker[MAX_N];

    scanf("%d", &n);
    for (i = 0; i < 2 * (n - 1); i += 2) {
        scanf("%d %d", &a[i][0], &a[i][1]);
        a[i + 1][1] = a[i][0];
        a[i + 1][0] = a[i][1];
    }

    qsort(a, 2 * (n - 1), sizeof(int) * 2, qsort_compare_int_x2);

    i = 0;
    for (j = 1; j <= n; j++) {
        if (a[i][0] == j) {a_marker[j] = i;}
        while(a[i][0] == j) {i++;}
    }

    for (i = 2; i <= n; i++) {
       if (i != 2) {printf(" ");}
       printf("%d", move(i, (n - 1) * 2, a, a_marker));
    }

    printf("\n");

    return 0;
}