CF793E.Problem of offices
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Earlier, when there was no Internet, each bank had a lot of offices all around Bankopolis, and it caused a lot of problems. Namely, each day the bank had to collect cash from all the offices.
Once Oleg the bank client heard a dialogue of two cash collectors. Each day they traveled through all the departments and offices of the bank following the same route every day. The collectors started from the central department and moved between some departments or between some department and some office using special roads. Finally, they returned to the central department. The total number of departments and offices was n, the total number of roads was n - 1. In other words, the special roads system was a rooted tree in which the root was the central department, the leaves were offices, the internal vertices were departments. The collectors always followed the same route in which the number of roads was minimum possible, that is 2_n_ - 2.
One of the collectors said that the number of offices they visited between their visits to offices a and then b (in the given order) is equal to the number of offices they visited between their visits to offices b and then a (in this order). The other collector said that the number of offices they visited between their visits to offices c and then d (in this order) is equal to the number of offices they visited between their visits to offices d and then c (in this order). The interesting part in this talk was that the shortest path (using special roads only) between any pair of offices among a, b, c and d passed through the central department.
Given the special roads map and the indexes of offices a, b, c and d, determine if the situation described by the collectors was possible, or not.
早些时候,在互联网尚未普及的年代,每家银行在 Bankopolis 市内都设有大量分支机构与营业网点,这带来了诸多问题。具体而言,银行每天都需要从所有分支机构和营业网点回收现金。
某日,银行客户 Oleg 听到了两名现金收缴员之间的一段对话。他们每天沿着完全相同的路线,遍历银行的所有部门与营业网点。收缴员们从中央部门出发,通过若干专用道路,在某些部门之间、或在某个部门与某个营业网点之间移动;最终,他们返回中央部门。整个银行(包括所有部门与营业网点)的总数为 n,而专用道路的总数为 n−1。换言之,这些专用道路构成了一棵以中央部门为根节点的有根树:其中叶节点代表营业网点,内部节点代表各部门。收缴员所遵循的路线始终是经过道路数最少的回路,即总长为 2n−2 条道路。
其中一名收缴员称:在按顺序先后访问营业网点 a 和 b 的过程中,他们所途经的营业网点数量,等于在按顺序先后访问营业网点 b 和 a 的过程中所途经的营业网点数量。另一名收缴员则称:在按顺序先后访问营业网点 c 和 d 的过程中,他们所途经的营业网点数量,等于在按顺序先后访问营业网点 d 和 c 的过程中所途经的营业网点数量。这段对话中引人注意之处在于:在 a、b、c、d 这四个营业网点中,任意一对营业网点之间的最短路径(仅允许使用专用道路)均必经过中央部门。
给定专用道路的地图(即上述有根树结构)以及营业网点 a、b、c、d 的编号,请判断收缴员所述的情形是否可能发生。
输入格式
The first line contains single integer n (5 ≤ n ≤ 5000) — the total number of offices and departments. The departments and offices are numbered from 1 to n, the central office has index 1.
The second line contains four integers a, b, c and d (2 ≤ a, b, c, d ≤ n) — the indexes of the departments mentioned in collector's dialogue. It is guaranteed that these indexes are offices (i.e. leaves of the tree), not departments. It is guaranteed that the shortest path between any pair of these offices passes through the central department.
On the third line n - 1 integers follow: _p_2, _p_3, ..., p__n (1 ≤ p__i < i), where p__i denotes that there is a special road between the i-th office or department and the p__i-th department.
Please note the joint enumeration of departments and offices.
It is guaranteed that the given graph is a tree. The offices are the leaves, the departments are the internal vertices.
第一行包含一个整数 n(5≤n≤5000)—— 办公室与部门的总数。部门和办公室编号为 1 到 n,其中中央办公室的编号为 1。
第二行包含四个整数 a、b、c 和 d(2≤a,b,c,d≤n)—— 收集者对话中提到的四个部门的编号。保证这些编号对应的是办公室(即树的叶子节点),而非部门。同时保证任意两个上述办公室之间的最短路径均经过中央部门。
第三行包含 n−1 个整数:p2, p3, …, pn(1≤pi<i),其中 pi 表示第 i 个办公室或部门与第 pi 个部门之间存在一条特殊道路。
请注意:部门与办公室采用统一编号。
保证所给图是一棵树;其中办公室为叶子节点,部门为内部顶点。
输出格式
If the situation described by the cash collectors was possible, print "YES". Otherwise, print "NO".
如果现金收集者所描述的情况是可能的,则输出 "YES";否则,输出 "NO"。
输入输出样例
输入#1
5 2 3 4 5 1 1 1 1
输出#1
YES
输入#2
10 3 8 9 10 1 2 2 2 2 2 1 1 1
输出#2
NO
输入#3
13 13 12 9 7 1 1 1 1 5 5 2 2 2 3 3 4
输出#3
YES
说明/提示
In the first example the following collector's route was possible:
. We can note that between their visits to offices a and b the collectors visited the same number of offices as between visits to offices b and a; the same holds for c and d (the collectors' route is infinite as they follow it each day).
In the second example there is no route such that between their visits to offices c and d the collectors visited the same number of offices as between visits to offices d and c. Thus, there situation is impossible.
In the third example one of the following routes is:
.
在第一个例子中,以下收件员路线是可行的:
。我们可以注意到:收件员在访问办公室 a 和 b 之间所经过的办公室数量,等于其在访问办公室 b 和 a 之间所经过的办公室数量;对办公室 c 和 d 同样成立(收件员的路线是无限循环的,因为他们每天均按此路线行进)。
在第二个例子中,不存在一条路线,使得收件员在访问办公室 c 和 d 之间所经过的办公室数量,等于其在访问办公室 d 和 c 之间所经过的办公室数量。因此,该情形不可能实现。
在第三个例子中,以下路线之一为:
。
输入解题思路,AI测评打分。不知道怎么写?