Huskar, Tuskar, and Balls
Difficulty: Hard

Huskar and Tuskar are playing a game with balls. There is a tree with $N$ nodes numbered from $1$ to $N$ respectively (root is $1$). There is a ball on each node. If a node has only one child, the ball on this node will drop onto the child node. But if a node has more than one child, Huskar and Tuskar can't decide the way that the ball will follow. So they start deleting edges one by one (Huskar goes first) until every ball has exactly one way to go. Every ball must be able to reach a leaf node. (A leaf node is a node which has no children.)

In this game each player has a node. When a ball drops onto a player's node, this player gets one point and the ball is removed from the tree. 

Both of them are *just* trying to maximize their own points. They are not jealous friends. So if one cannot affect his own points, he would like to help increase his friend's points.

You are given a graph, and you need to determine Huskar and Tuskar's points when they delete edges optimally for themselves.

Input Format:
Line 1 : Two integers : $N$ and $Q$, the number of the nodes in the tree and the number of queries.  
Lines $2...N$ : line $i$, one integer : the parent of node $i$.  
Lines $N+1...N+Q$ : line $(N+i)$ , two integers : Huskar's node and Tuskar's node.  


**Constraints**  
$2 \le N \le 2 \times 10^5$  
$1 \le Q \le 2 \times 10^5$  
Huskar and Tuskar can't be on same node.  

Output Format:
You must print $Q$ lines. In each line, two integers for each query : Huskar's points and Tuskar's points.

Sample Input:
	6 1
	4
	1
	1
	3
	3
	3 6

Sample Output:
	2 1

Python 3 starter code:


Reference solution (JavaScript):
    program sparseTableSolution; // Complexity - O(N log N + Q)
    uses math;

    const	MAXN =	500000;

    type	tEdge =	record
                        v, next: longInt;
                    end;

    var	first, depth, dfs_id, dfs_order, max2pow, stack, parent: array[ 1..2 * MAXN ] of longInt;
        e: array[ 1..MAXN ] of tEdge;
        st: array[ 1..2 * MAXN, 0..20 ] of longInt;

    procedure addEdge( u, v: longInt );
    const	ee: longInt = 0;
    begin
        inc( ee );
        e[ee].v := v;
        e[ee].next := first[u];
        first[u] := ee;
    end;

    procedure dfs( v, d: longInt );
    const	id: longInt = 0;
    var	i: longInt;
    begin
        inc( id );
        depth[v] := d;
        dfs_id[v] := id;
        dfs_order[id] := v;

        i := first[v];
        while ( i > 0 ) do
        begin
            dfs( e[i].v, d + 1 );

            inc( id );
            dfs_order[id] := v;

            i := e[i].next;
        end;
    end;

    procedure nonRecursiveDFS( v: longInt );
    var	i, r, id: longInt;
    begin
        id := 0;
        depth[v] := 1;
        parent[v] := 0;
        r := 1;
        stack[1] := v;

        while ( r > 0 ) do
        begin
            v := stack[r];
            dec( r );

            while ( depth[ dfs_order[id] ] >= depth[v] ) do
            begin
                inc( id );
                dfs_order[id] := parent[ dfs_order[id - 1] ];
            end;

            inc( id );
            dfs_order[id] := v;
            dfs_id[v] := id;

            i := first[v];
            while ( i > 0 ) do
            begin
                if ( parent[v] <> e[i].v ) then
                begin
                    depth[ e[i].v ] := depth[v] + 1;
                    parent[ e[i].v ] := v;
                    inc( r );
                    stack[r] := e[i].v;
                end;

                i := e[i].next;
            end;
        end;
    end;

    function minNode( u, v: longInt ): longInt;
    begin
        if ( depth[u] < depth[v] ) then
            exit( u )
        else
            exit( v );
    end;

    procedure prepareSparseTable( sz: longInt );
    var	i, j: longInt;
    begin
        max2pow[1] := 0;
        for i := 2 to sz do
        begin
            max2pow[i] := max2pow[i - 1];

            if ( i = 2 shl max2pow[i] ) then
                inc( max2pow[i] );
        end;

        for i := 1 to sz do
            st[i][0] := dfs_order[i];

        for j := 0 to 19 do
            for i := 1 to sz - (2 shl j) + 1 do
                st[i][j + 1] := minNode( st[i][j], st[i + 1 shl j][j] );
    end;

    function lca( u, v: longInt ): longInt;
    var	l, r, p: longInt;
    begin
        l := min( dfs_id[u], dfs_id[v] );
        r := max( dfs_id[u], dfs_id[v] );
        p := max2pow[r - l + 1];

        exit( minNode( st[l][p], st[r - 1 shl p + 1][p] ) );
    end;

    var	n, q, i, p, u, v: longInt;

    begin
        readln( n, q );
        for i := 2 to n do
        begin
            readln( p );

            addEdge( p, i );
        end;

    //	dfs( 1, 1 );
    //  recursive dfs may cause RTE due to low stack memory (8 MB)
    
        nonRecursiveDFS( 1 );

        prepareSparseTable( 2 * n );

        for i := 1 to q do
        begin
            readln( u, v );

            writeln( depth[u], ' ', depth[v] - depth[ lca(u, v) ] );
        end;
    end.