10711071 SUSTech Online Judge
Problem 1071 --Binary search tree

1071: Binary search tree

Time Limit: 1 Sec  Memory Limit: 512 MB
Submit: 398  Solved: 178
[Submit][Status][Web Board]

Description

There are a lot of sequences, and each sequence can represent a binary search, please judge whether two sequences can form the same binary search tree.

 

The first value is the root of the tree, and then you can add the value into the tree following the rules of binary search tree.

Input

The first line will be an integer T, which is the number of test cases. (1 <= T <= 100). For each test case, the first line will be two integers n(1 <= n <= 100) and m(1 <= m <= 63), n is the number of judgement, m is the length of the sequences. The second line will be m integer, a1……am(1 <= ai <= 109), it is the standard sequences. Then followed by n lines, each line will be another arrangement of the standard sequences.

Output

For each judgement, print YES when a arrangement of the standard sequences can form the same binary search tree with the standard sequences, else print NO.

Sample Input

1
2 6
5 6 7 4 3 2 
5 4 3 2 6 7 
5 7 6 3 4 2

Sample Output

YES
NO

HINT

Source

 

[Submit][Status]