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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    List<Integer>[] g;

    int[] maxDist;
    int[] firstEdgeS;

    void go(int v, int p, int curMaxDist, int firstEdge) {
        dfsOrder.add(v);
        maxDist[v] = curMaxDist;
        firstEdgeS[v] = firstEdge;
        for (int to : g[v]) {
            if (to == p) {
                continue;
            }
            if (v > curMaxDist) {
                go(to, v, v, to);
            } else {
                go(to, v, curMaxDist, firstEdge);
            }
        }
    }

    int getCnt(int v, int shouldBeLess, int p) {
        int res = 1;
        for (int to : g[v]) {
            if (to == p || to >= shouldBeLess) {
                continue;
            }
            res += getCnt(to, shouldBeLess, v);
        }
        return res;
    }

    List<Integer> dfsOrder = new ArrayList<>();

    int[] p;
    int[] sz;

    int get(int x) {
        return p[x] == x ? x : (p[x] = get(p[x]));
    }

    void join(int x, int y) {
        x = get(x);
        y = get(y);
        if (x != y) {
            p[x] = y;
            sz[y] += sz[x];
        }
    }

    void solve() {
        int n = in.nextInt();
        g = new List[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i + 1 < n; i++) {
            int fr = in.nextInt() - 1;
            int to = in.nextInt() - 1;
            g[fr].add(to);
            g[to].add(fr);
        }
        maxDist = new int[n];
        firstEdgeS = new int[n];
        go(0, -1, -1, -1);
        int[] ans = new int[n];
        ans[0] = 1;

        List<Event> events = new ArrayList<>();
        for (int startV : dfsOrder) {
            if (startV == 0) {
                continue;
            }
            int goTo = maxDist[startV];
            events.add(new Event(startV, startV, goTo, 1));
            int u = firstEdgeS[startV];
            int next = maxDist[goTo];
            if (u < next) {
                events.add(new Event(startV, u, next, -1));
            }
        }

        p = new int[n];
        sz = new int[n];
        for (int i = 0; i < n; i++) {
            p[i] = i;
            sz[i] = 1;
        }
        Collections.sort(events);
        int it = 0;
        int[] time =new int[n];
        Arrays.fill(time, -1);
        for (int v = 0; v < n; v++) {
            while (it < events.size() && events.get(it).shouldBeLess == v) {
                int curVertex = events.get(it).startVertex;
                int sumSize = sz[get(curVertex)];
                time[get(curVertex)] = it;
                for (int u : g[curVertex]) {
                    if (u < v) {
                        int oo = get(u);
                        if (time[oo] != it) {
                            time[oo] = it;
                            sumSize += sz[oo];
                        }
                    }
                }
                ans[events.get(it).whereToAdd] += events.get(it).mul * sumSize;
                it++;
            }
        }

        for (int startV : dfsOrder) {
            if (startV == 0) {
                continue;
            }
            int goTo = maxDist[startV];
            ans[startV] += ans[goTo];
        }
        for (int v = 1; v < n; v++) {
            out.print((ans[v] - 1) + " ");
        }
    }

    class Event implements Comparable<Event>{
        int whereToAdd;
        int startVertex;
        int shouldBeLess;
        int mul;

        public Event(int whereToAdd, int startVertex, int shouldBeLess, int mul) {
            this.whereToAdd = whereToAdd;
            this.startVertex = startVertex;
            this.shouldBeLess = shouldBeLess;
            this.mul = mul;
        }

        @Override
        public int compareTo(Event o) {
            return Integer.compare(shouldBeLess, o.shouldBeLess);
        }
    }

    void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}