A14725 | Squid Game
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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!
$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.
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$ .
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:
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted