CF634A.Island Puzzle
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
Determine if it is possible for the islanders to arrange the statues in the desired order.
一个遥远的群岛链包含 n 个岛屿,编号为 1 至 n。双向桥梁将这些岛屿连接成一个简单环——即桥梁连接岛屿 1 和 2、岛屿 2 和 3,依此类推,此外还有一座桥梁连接岛屿 n 和 1。每个岛屿的中心都有一座完全相同的基座,其中除一座岛屿外,其余每座岛屿的基座上均放置着一座易碎的、颜色唯一的雕像。剩下的一座岛屿上仅有一个空基座。
岛民希望将雕像重新排列成一种新的顺序。为此,他们重复执行以下操作:首先,选择一座与空基座所在岛屿直接相邻的岛屿;然后,小心翼翼地将该岛屿基座上的雕像经由相连的桥梁搬运过去,并将其放置在空基座上。
请判断岛民是否能够将雕像排列成所期望的顺序。
输入格式
The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.
The second line contains n space-separated integers a__i (0 ≤ a__i ≤ n - 1) — the statue currently placed on the i-th island. If a__i = 0, then the island has no statue. It is guaranteed that the a__i are distinct.
The third line contains n space-separated integers b__i (0 ≤ b__i ≤ n - 1) — the desired statues of the _i_th island. Once again, b__i = 0 indicates the island desires no statue. It is guaranteed that the b__i are distinct.
第一行包含一个整数 n(2≤n≤200000)—— 岛屿的总数。
第二行包含 n 个用空格分隔的整数 ai(0≤ai≤n−1)—— 当前第 i 座岛屿上放置的雕像编号。若 ai=0,表示该岛屿上没有雕像。保证所有 ai 互不相同。
第三行包含 n 个用空格分隔的整数 bi(0≤bi≤n−1)—— 第 i 座岛屿期望拥有的雕像编号。同样地,bi=0 表示该岛屿不希望有任何雕像。保证所有 bi 互不相同。
输出格式
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
如果可以在现有网络中完成重排,则输出 "YES"(不带引号);否则输出 "NO"。
输入输出样例
输入#1
3 1 0 2 2 0 1
输出#1
YES
输入#2
2 1 0 0 1
输出#2
YES
输入#3
4 1 2 3 0 0 3 2 1
输出#3
NO
说明/提示
In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2.
In the third sample, no sequence of movements results in the desired position.
在第一个样例中,岛民可以首先将雕像 1 从岛屿 1 移动到岛屿 2,然后将雕像 2 从岛屿 3 移动到岛屿 1,最后将雕像 1 从岛屿 2 移动到岛屿 3。
在第二个样例中,岛民只需将雕像 1 从岛屿 1 移动到岛屿 2 即可。
在第三个样例中,不存在任何移动序列能够达到目标位置。
输入解题思路,AI测评打分。不知道怎么写?