I Want My Permutation!: Input
I Want My Permutation!: Input
I Want My Permutation!
Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes
Given an arrangement of N integers you need to arrive at another user defined permutation of the same
integers. You are given an initial permutation, final permutation and pairs of numbers you can swap for
the given task. Your goal is to tell if at all it is possible to reach from initial to the final permutation
using the allowed swaps. You may use a swap multiple times. See the sample test case for more clarity.
For the sake of simplicity assume that these integers are 1, 2, 3,....,N.
Input
You are given T in the first line denoting the number of test cases. Each test case consists of 2 integers
in the first line N and M where N stands for the number of integers in the permutation and M stands for
the number of allowed permutations. This is followed by a line consisting of N space separated integers
denoting the initial permutation. The next line again contains N space separated integers denoting the
final permutation. This is followed by M lines each consisting of 2 space separated integers (xi , yi ) denoting
that the integers xi and yi can be swapped.
Output
For each test case you need to output YES if you can construct the final permutation, NO otherwise
Sample Input:
2
41
1324
1423
34
41
1324
1423
24
Sample Output:
YES
NO
Easy Constraints:
• 2 ≤ N ≤ 100
• 1 ≤ M ≤ 100
• 1 ≤ T ≤ 10
Medium Constraints:
• 2 ≤ N ≤ 105
• 1 ≤ M ≤ 105
Page 1 of 2
,
• 1 ≤ T ≤ 10
Page 2 of 2