309004: CF1610H. Squid Game

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Squid Game

题意翻译

猫猫们看了鱿鱼游戏,对其很感兴趣,决定自己举办一个猫猫游戏。 有 $m$ 只猫猫参与了游戏,游戏失败的猫猫要被拉出去和铃酱贴贴。 蓝是游戏的裁判,由于所有猫猫都想和铃酱贴贴,于是她要淘汰所有的猫猫。下面是她淘汰猫猫的方式: - 有一棵 $n$ 个结点的无根树,每只猫猫都有两个特殊结点 $x_i$ 与 $y_i$。 - 在一次操作中,蓝可以选择某个结点 $v$。接下来,对于所有未被淘汰的猫猫 $i$,蓝将找到一个结点 $w$,满足其在 $x_i$ 到 $y_i$ 的简单路径上且距离 $v$ 最近,若 $w\neq x_i$ 且 $w\neq y_i$,那么猫猫 $i$ 将会被淘汰。 现在蓝想要知道她至少要进行多少次操作才能淘汰所有的猫猫。如果无论如何都不能淘汰所有的猫猫,请输出 $-1$。 输入数据的第一行含两个数 $n$ 与 $m$,意义同上所述。第二行含 $n-1$ 个数 $par_2,par_3,\dots,par_n$,其中 $par_i$ 代表无根树上存在一条连接 $i$ 与 $par_i$ 的边。接下来 $m$ 行,第 $i$ 行含两个数 $x_i$ 与 $y_i$,意义同上所述。 本题数据满足:$1 \leq n,m \leq 3\times10^5,1 \leq par_i<i,1 \leq x_i,y_i \leq n,x_i\neq y_i$

题目描述

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!

输入输出格式

输入格式


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.

输出格式


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

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: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1610H/2f92b74ef3689103fc27552fedd0869997b9492b.png)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

题意翻译

猫猫们看了鱿鱼游戏,对其很感兴趣,决定自己举办一个猫猫游戏。 有 $m$ 只猫猫参与了游戏,游戏失败的猫猫要被拉出去和铃酱贴贴。 蓝是游戏的裁判,由于所有猫猫都想和铃酱贴贴,于是她要淘汰所有的猫猫。下面是她淘汰猫猫的方式: - 有一棵 $n$ 个结点的无根树,每只猫猫都有两个特殊结点 $x_i$ 与 $y_i$。 - 在一次操作中,蓝可以选择某个结点 $v$。接下来,对于所有未被淘汰的猫猫 $i$,蓝将找到一个结点 $w$,满足其在 $x_i$ 到 $y_i$ 的简单路径上且距离 $v$ 最近,若 $w\neq x_i$ 且 $w\neq y_i$,那么猫猫 $i$ 将会被淘汰。 现在蓝想要知道她至少要进行多少次操作才能淘汰所有的猫猫。如果无论如何都不能淘汰所有的猫猫,请输出 $-1$。 输入数据的第一行含两个数 $n$ 与 $m$,意义同上所述。第二行含 $n-1$ 个数 $par_2,par_3,\dots,par_n$,其中 $par_i$ 代表无根树上存在一条连接 $i$ 与 $par_i$ 的边。接下来 $m$ 行,第 $i$ 行含两个数 $x_i$ 与 $y_i$,意义同上所述。 本题数据满足:$1 \leq n,m \leq 3\times10^5,1 \leq par_i<i,1 \leq x_i,y_i \leq n,x_i\neq y_i$

加入题单

上一题 下一题 算法标签: