Robots
Time limit2sMemory limit256 MB
Decide whether some sequence of red and green moves brings robots starting on every intersection together onto one intersection.
Problem
While you were not watching, your robots started moving on their own and spread across your hometown. The town has intersections numbered 0 through , and exactly one robot stands on each intersection. Intersection carries one red signpost that points at an intersection and one green signpost that points at an intersection .
When you press the red button on the remote control, every robot moves along the red signpost at the same time: a robot on intersection moves to . When you press the green button, every robot moves to instead. Write a program that decides whether some sequence of button presses brings all robots to the same intersection at the same time.
Input
The first line contains the number of data sets (). The data sets are independent and are processed the same way.
Each data set consists of three lines.
- The first line contains the data set number and the number of intersections . The data sets are numbered 1 through in order.
- The second line contains separated by spaces (, ).
- The third line contains separated by spaces (, ).
, and the sum of over all data sets is at most 2000. Both signposts on an intersection may point the same way, so is possible.
Output
Print one line per data set. The line contains the data set number , one space, then YES if you can gather every robot on one intersection, or NO otherwise.
Hint
In the second data set of the example, pressing GREEN, RED, RED, GREEN gathers every robot on intersection 2.