301342: CF251B. Playing with Permutations

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

Description

Playing with Permutations

题意翻译

现在你有一个长为 $n$ 的序列 $q$ 以及另外一个长度为 $n$ 的序列 $p$ 。其中 $p$ 序列初始化为 $1,2,3,....n$ 定义两种操作: $1.p[q[i]]=p[i]$ $2.p[i]=p[q[i]]$ 给出步骤数 $k$ 和目标序列 $s$ ,问你能否在恰好 $k$ 个操作后实现从 $p$ 序列到 $s$ 序列的转换? 输出"YES"或者"NO"(不含引号)来表示你的结果。 要求:在 $k$ 步以前的过程中不能让 $p=s$ ,否则视作失败,哪怕是刚开始 $p=s$ 也不行。 输入格式: 第一行两个整数 $n,k$ ($1<=n,k<=100$) 第二行以空格隔开的 $n$ 个数字,表示序列 $q$ 第三行以空格隔开的 $n$ 个数字,表示序列 $s$ 输出格式 仅一行,"YES"或"NO"(不含引号)表示结果。

题目描述

Little Petya likes permutations a lot. Recently his mom has presented him permutation $ q_{1},q_{2},...,q_{n} $ of length $ n $ . A permutation $ a $ of length $ n $ is a sequence of integers $ a_{1},a_{2},...,a_{n} $ $ (1<=a_{i}<=n) $ , all integers there are distinct. There is only one thing Petya likes more than permutations: playing with little Masha. As it turns out, Masha also has a permutation of length $ n $ . Petya decided to get the same permutation, whatever the cost may be. For that, he devised a game with the following rules: - Before the beginning of the game Petya writes permutation $ 1,2,...,n $ on the blackboard. After that Petya makes exactly $ k $ moves, which are described below. - During a move Petya tosses a coin. If the coin shows heads, he performs point 1, if the coin shows tails, he performs point 2. 1. Let's assume that the board contains permutation $ p_{1},p_{2},...,p_{n} $ at the given moment. Then Petya removes the written permutation $ p $ from the board and writes another one instead: $ p_{q1},p_{q2},...,p_{qn} $ . In other words, Petya applies permutation $ q $ (which he has got from his mother) to permutation $ p $ . 2. All actions are similar to point 1, except that Petya writes permutation $ t $ on the board, such that: $ t_{qi}=p_{i} $ for all $ i $ from 1 to $ n $ . In other words, Petya applies a permutation that is inverse to $ q $ to permutation $ p $ . We know that after the $ k $ -th move the board contained Masha's permutation $ s_{1},s_{2},...,s_{n} $ . Besides, we know that throughout the game process Masha's permutation never occurred on the board before the $ k $ -th move. Note that the game has exactly $ k $ moves, that is, throughout the game the coin was tossed exactly $ k $ times. Your task is to determine whether the described situation is possible or else state that Petya was mistaken somewhere. See samples and notes to them for a better understanding.

输入输出格式

输入格式


The first line contains two integers $ n $ and $ k $ ( $ 1<=n,k<=100 $ ). The second line contains $ n $ space-separated integers $ q_{1},q_{2},...,q_{n} $ ( $ 1<=q_{i}<=n $ ) — the permutation that Petya's got as a present. The third line contains Masha's permutation $ s $ , in the similar format. It is guaranteed that the given sequences $ q $ and $ s $ are correct permutations.

输出格式


If the situation that is described in the statement is possible, print "YES" (without the quotes), otherwise print "NO" (without the quotes).

输入输出样例

输入样例 #1

4 1
2 3 4 1
1 2 3 4

输出样例 #1

NO

输入样例 #2

4 1
4 3 1 2
3 4 2 1

输出样例 #2

YES

输入样例 #3

4 3
4 3 1 2
3 4 2 1

输出样例 #3

YES

输入样例 #4

4 2
4 3 1 2
2 1 4 3

输出样例 #4

YES

输入样例 #5

4 1
4 3 1 2
2 1 4 3

输出样例 #5

NO

说明

In the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before $ k $ moves were performed. In the second sample the described situation is possible, in case if after we toss a coin, we get tails. In the third sample the possible coin tossing sequence is: heads-tails-tails. In the fourth sample the possible coin tossing sequence is: heads-heads.

Input

题意翻译

现在你有一个长为 $n$ 的序列 $q$ 以及另外一个长度为 $n$ 的序列 $p$ 。其中 $p$ 序列初始化为 $1,2,3,....n$ 定义两种操作: $1.p[q[i]]=p[i]$ $2.p[i]=p[q[i]]$ 给出步骤数 $k$ 和目标序列 $s$ ,问你能否在恰好 $k$ 个操作后实现从 $p$ 序列到 $s$ 序列的转换? 输出"YES"或者"NO"(不含引号)来表示你的结果。 要求:在 $k$ 步以前的过程中不能让 $p=s$ ,否则视作失败,哪怕是刚开始 $p=s$ 也不行。 输入格式: 第一行两个整数 $n,k$ ($1<=n,k<=100$) 第二行以空格隔开的 $n$ 个数字,表示序列 $q$ 第三行以空格隔开的 $n$ 个数字,表示序列 $s$ 输出格式 仅一行,"YES"或"NO"(不含引号)表示结果。

加入题单

上一题 下一题 算法标签: