Little Yogi and Lego Trains

0

0 votes
Easy
Problem

As a present for birthday, Yogi bought for his son two Lego Trains. The little Yogi quickly became addict to the game. He usually spends a lot of time playing with them; no wonder why he is good at creating complicated Train Structures.
A Lego train consists of N coaches and each coach is having a unique id.
Now Yogi wants to check his son's intelligence so he gave him a task.The task is "Convert Lego_train_1 to Lego_train_2".

Two trains would be similar if the position of corresponding coaches is the same.
For this Yogi gave his son n pairs of good integers(ai, bi). Little yogi can perform following operation with Lego_train_1:

  •     Swap Coach(x) and Coach(y) only if (x, y) is a good pair.

Help little Yogi and tell if he can convert lego_train_1 to lego_train_2 using such operations.

Input format:

The first line of input will contain an integer T, denoting the number of test cases.
Each test case starts with two space-separated integers N and M denoting total coaches in each train and total good pairs.
The next line contains N space-separated integers denoting coach_ids for Lego_train_1.
The next line contains N space-separated integers denoting coach_ids for Lego_train_2.
Each of the next M lines contains two space-separated integers ai and bi denoting a good pair.

Output format:

For every test case output "YES" (without quotes) if Little Yogi can perform the task and "NO" otherwise.

Constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 105
1 ≤ M ≤ 105
1 ≤ coach_ids(i) ≤ N.
1 ≤ ai < bi ≤ N
 

 

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Test case 1:

We can only swap coach 3 and coach 4. On swapping the coaches, the order changed to  1,3,4,2  which is not similar to Lego_Train_2.

Editor Image

?