0% found this document useful (0 votes)
58 views2 pages

I Want My Permutation!: Input

The document describes an algorithmic problem where given an initial permutation of integers, a final permutation, and allowed swaps between integers, the task is to determine if the final permutation can be reached from the initial permutation using the allowed swaps. The input specifies the number of test cases, each with the number of integers N, number of allowed swaps M, the initial and final permutations as space separated integers, and M lines specifying the allowed swaps as integer pairs. The output for each test case is YES if the final permutation can be reached or NO otherwise. Sample input and output are provided.

Uploaded by

Puneet Jindal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

I Want My Permutation!: Input

The document describes an algorithmic problem where given an initial permutation of integers, a final permutation, and allowed swaps between integers, the task is to determine if the final permutation can be reached from the initial permutation using the allowed swaps. The input specifies the number of test cases, each with the number of integers N, number of allowed swaps M, the initial and final permutations as space separated integers, and M lines specifying the allowed swaps as integer pairs. The output for each test case is YES if the final permutation can be reached or NO otherwise. Sample input and output are provided.

Uploaded by

Puneet Jindal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

,

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

• 1 ≤ Each integer ≤ 109

Medium Constraints:

• 2 ≤ N ≤ 105

• 1 ≤ M ≤ 105

Page 1 of 2
,

• 1 ≤ T ≤ 10

• 1 ≤ Each integer ≤ 109

Page 2 of 2

You might also like