Codeforces Global Round 17
H
Squid Game
Codeforces
3100
原题
← GI →
翻译中…

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

翻译中…

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 \lt 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

翻译中…

Print the minimum number of operations Mashtali has to perform.

翻译中…

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

翻译中…

Note

翻译中…

Explanation for the first sample:

翻译中…

image 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.

翻译中…

样例

Input #1
6 3
1 1 1 4 4
1 5
3 4
2 6
Output #1
2
InputCopy OutputCopy
Input #2
5 3
1 1 3 3
1 2
1 4
1 5
Output #2
-1
InputCopy OutputCopy
题目信息
题号
1610H
时间限制
2 s
内存限制
256 MB
标签
数据结构
DFS 及类似
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。