Squid Game
Contest: Codeforces Global Round 17
Difficulty rating: 3100
Tags: data structures, dfs and similar, greedy, trees

After watching the new over-rated series Squid Game, Mashtali and Soroush decided to hold their own Squid Games! Soroush agreed to be the host and will provide money for the winner's prize, and Mashtali became the Front Man!

$$$m$$$ players registered to play in the games to win the great prize, but when Mashtali found out how huge the winner's prize is going to be, he decided to kill eliminate all the players so he could take the money for himself!

Here is how evil Mashtali is going to eliminate players:

There is an unrooted tree with $$$n$$$ vertices. Every player has $$$2$$$ special vertices $$$x_i$$$ and $$$y_i$$$.

In one operation, Mashtali can choose any vertex $$$v$$$ of the tree. Then, for each remaining player $$$i$$$ he finds a vertex $$$w$$$ on the simple path from $$$x_i$$$ to $$$y_i$$$, which is the closest to $$$v$$$. If $$$w\ne x_i$$$ and $$$w\ne y_i$$$, player $$$i$$$ will be eliminated.

Now Mashtali wondered: "What is the minimum number of operations I should perform so that I can remove every player from the game and take the money for myself?"

Since he was only thinking about the money, he couldn't solve the problem by himself and asked for your help!

Input format:
The first line contains $$$2$$$ integer $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 3 \cdot 10^5)$$$ — the number of vertices of the tree and the number of players.

The second line contains $$$n-1$$$ integers $$$par_2, par_3, \ldots, par_n$$$ $$$(1 \le par_i < i)$$$ — denoting an edge between node $$$i$$$ and $$$par_i$$$.

The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ $$$(1 \le x_i, y_i \le n, x_i \ne y_i)$$$ — the special vertices of the $$$i$$$-th player.

Output format:
Print the minimum number of operations Mashtali has to perform.

If there is no way for Mashtali to eliminate all the players, print $$$-1$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Explanation for the first sample:

In the first operation, Mashtali can choose vertex $$$1$$$ and eliminate players with colors red and blue. In the second operation, he can choose vertex $$$6$$$ and eliminate the player with orange color.

In the second sample, Mashtali can't eliminate the first player.

Example input:
6 3
1 1 1 4 4
1 5
3 4
2 6

Example output:
2

Example input:
5 3
1 1 3 3
1 2
1 4
1 5

Example output:
-1

Editorial:
A player gets eliminated iff $$$(x_i, y_i)$$$ is a cross-edge while rooting the tree from where Mashtali sits. we say a vertex is marked if Mashtali choses it in a move.
Lets solve the problem in polynomial time. First, lets fix one of the marked vertices and root the tree from it. Then all cross-edges are already covered and we have a set of back-edges to cover. Then, we can use this greedy approach: At each step, take the lowest uncovered back-edge and mark the second highest vertex in its path.(the highest one is either $$$x_i$$$ or $$$y_i$$$ which is not allowed) You can prove its correct using the fact that there is no lower back-edges, so if we mark a higher vertex all vertices that were marked before, are marked now.
So far the complexity is smth like $$$O(n^3)$$$. But we can use fenwick-tree to check whether an edge is covered or not. Which leads to a $$$O(n^2 \cdot log(n))$$$ solution.
Now, let's forget about fixing one of the moves! let's just make the tree rooted at $$$1$$$, ignore all cross-edges, and do the previous solution. Finally, check if there is an uncovered cross-edge. If so, just put a token on $$$1$$$.
Now, why is that correct? if all cross-edges are covered, everything is ok. So we just need to show we need more marked vertices if a cross-edge is uncovered. If that happens, it means all our tokens are either in subtree of $$$x_i$$$ or in subtree of $$$y_i$$$. Since we put our tokens at highest possible nodes, there is no vertex that would mark the cross-edge and a back-edge with it. So, we need to mark at least one more vertex and the root, suffice. time complexity: $$$O(n \cdot log(n))$$$