CF1610H.Squid Game
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:256MB
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!
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 xi and yi.
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 xi to yi, which is the closest to v. If w=xi and w=yi, 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!
看完大热(但被过度吹捧)的剧集《鱿鱼游戏》后,Mashtali 和 Soroush 决定举办属于他们自己的“鱿鱼游戏”!Soroush 同意担任主持人,并为获胜者提供奖金;而 Mashtali 则成了“面具人”(Front Man)!
共有 m 名玩家报名参加游戏,以争夺巨额奖金。但当 Mashtali 得知奖金数额如此巨大后,他决定将所有玩家全部“淘汰”,从而独吞这笔奖金!
以下是 Mashtali 这位邪恶角色淘汰玩家的方式:
给定一棵含 n 个顶点的无根树。每名玩家 i 都有两个特殊顶点 xi 和 yi。
在一次操作中,Mashtali 可任选树上的一个顶点 v。接着,对每一位尚未被淘汰的玩家 i,他在 xi 到 yi 的唯一简单路径上找出距离 v 最近的顶点 w。若 w=xi 且 w=yi,则该玩家 i 将被淘汰。
现在 Mashtali 想知道:“我至少需要执行多少次操作,才能将所有玩家全部淘汰,从而独享奖金?”
由于他满脑子只想着钱,自己无法解决这个问题,于是向你求助!
输入格式
The first line contains 2 integer n and m (1≤n,m≤3⋅105) — the number of vertices of the tree and the number of players.
The second line contains n−1 integers par2,par3,…,parn (1≤pari<i) — denoting an edge between node i and pari.
The i-th of the following m lines contains two integers xi and yi (1≤xi,yi≤n,xi=yi) — the special vertices of the i-th player.
第一行包含两个整数 n 和 m(1≤n,m≤3⋅105)—— 分别表示树的顶点数和玩家数。
第二行包含 n−1 个整数 par2,par3,…,parn(1≤pari<i)—— 表示节点 i 与 pari 之间存在一条边。
接下来的 m 行中,第 i 行包含两个整数 xi 和 yi(1≤xi,yi≤n,xi=yi)—— 表示第 i 位玩家的两个特殊顶点。
输出格式
Print the minimum number of operations Mashtali has to perform.
If there is no way for Mashtali to eliminate all the players, print −1.
输出Mashtali需要执行的最少操作次数。
如果Mashtali无法淘汰所有玩家,则输出 −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.
第一个样例的解释:
在第一次操作中,Mashtali 可选择顶点 1,从而淘汰红色和蓝色的玩家。在第二次操作中,他可选择顶点 6,从而淘汰橙色的玩家。
在第二个样例中,Mashtali 无法淘汰第一位玩家。
输入解题思路,AI测评打分。不知道怎么写?