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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
clock_t __my_start_clock = clock();
#else
#define eprintf(...)
#endif

#define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)
#define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)

using namespace std;

#ifdef LOCAL
static struct __timestamper {
  string what;
  __timestamper(const char* what) : what(what){};
  __timestamper(const string& what) : what(what){};
  ~__timestamper(){
    TIMESTAMPf("%s", what.data());
	}
} __TIMESTAMPER("end");
#else 
struct __timestamper {};
#endif

typedef long long ll;
typedef long double ld;

struct dsu {
  vector<int> p;
  vector<int> size;
  dsu(int n) : p(n), size(n) {
    for (int i = 0; i < n; i++) {
      p[i] = i;
      size[i] = 1;
    }
  }

  int get(int v) {
    if (v == p[v]) return v;
    return p[v] = get(p[v]);
  }

  void join(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b) return;
    if (size[a] > size[b]) swap(a, b);
    p[a] = b;
    size[b] += size[a];
  }

  int is_same(int a, int b) {
    return get(a) == get(b);
  }

  int get_size(int a) {
    return size[get(a)];
  }
};

vector<vector<int>> g;
dsu d(0);
vector<int> ans, maxv;

int find_less(int u, int p, int lim) {
  if (u > lim) {
    return 0;
  }
  int ans = 1;
  for (int v : g[u]) {
    if (v != p && v < lim && d.is_same(v, u)) {
      ans += find_less(v, u, lim);
    }
  }
  return ans;
}

void set_ans(int u, int p, int a, int m) {
  ans[u] = a;
  maxv[u] = m;
  for (int v : g[u]) {
    if (v != p && d.is_same(v, u)) {
      set_ans(v, u, a, m);
    }
  }
}

int main() {
#ifdef LOCAL
  freopen("e.in", "r", stdin);
  freopen("e.out", "w", stdout);
#endif

  int n;
  scanf("%d", &n);
  g = vector<vector<int>> (n);
  d = dsu(n);

  ans = vector<int> (n, -1);
  maxv = vector<int> (n, -1);

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  ans[0] = 0;
  maxv[0] = 0;

  for (int v = 1; v < n; v++) {
    int up = -1;
    for (int u : g[v]) {
      if (ans[u] != -1) {
        assert(up == -1);
        up = u;
      }
    }
    if (up != -1) {
      int m = maxv[up];
      ans[v] = ans[up] + 1;
      maxv[v] = v;
      for (int u : g[v]) {
        if (u < v && u != up) {
          ans[u] = find_less(u, -1, m);
          ans[v] += ans[u];
        }
      }
      for (int u : g[v]) {
        if (u < v && u != up) {
          set_ans(u, -1, ans[v] + d.get_size(u) - ans[u], v);
        }
      }
    }
    for (int u : g[v]) {
      if (u < v) {
        d.join(u, v);
      }
    }
  }

  for (int i = 1; i < n; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");

  return 0;
}