CF1610H.Squid Game

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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!

mm 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 nn vertices. Every player has 22 special vertices xix_i and yiy_i .

In one operation, Mashtali can choose any vertex vv of the tree. Then, for each remaining player ii he finds a vertex ww on the simple path from xix_i to yiy_i , which is the closest to vv . If wxiw\ne x_i and wyiw\ne y_i , player ii 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!

输入格式

The first line contains 22 integer nn and mm (1n,m3105)(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 n1n-1 integers par2,par3,,parnpar_2, par_3, \ldots, par_n (1pari<i)(1 \le par_i < i) — denoting an edge between node ii and paripar_i .

The ii -th of the following mm lines contains two integers xix_i and yiy_i (1xi,yin,xiyi)(1 \le x_i, y_i \le n, x_i \ne y_i) — the special vertices of the ii -th player.

输出格式

Print the minimum number of operations Mashtali has to perform.

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

输入输出样例

  • 输入#1

    6 3
    1 1 1 4 4
    1 5
    3 4
    2 6

    输出#1

    2
  • 输入#2

    5 3
    1 1 3 3
    1 2
    1 4
    1 5

    输出#2

    -1

说明/提示

Explanation for the first sample:

In the first operation, Mashtali can choose vertex 11 and eliminate players with colors red and blue. In the second operation, he can choose vertex 66 and eliminate the player with orange color.In the second sample, Mashtali can't eliminate the first player.

首页