Days of the week
InterviewTime limit1sMemory limit1024 MB
Each button shifts its linked universes forward one weekday (mod 7). Decide whether every weekday configuration is reachable, and print an unreachable one if not.
- Level
Medium5 of 10
- Topics
- Matrix, Math, Implementation
- Solved
- No attempts yet
Problem
The Architect has created a multiverse with universes. In each universe, the day of the week has been set individually. The Architect also has buttons. Each button is connected to a certain set of universes. Pressing a button shifts all of its connected universes one day forward.
The Architect is curious: is there such a configuration of weekdays in universes that it is unachievable by any sequence of pressing buttons? Help the Architect solve this problem.
All universes use the Earth week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. When shifting the day of the week forward, Monday changes to Tuesday, Tuesday to Wednesday, and so on, and Sunday changes to Monday.
The Architect can press any button any number of times and in any order. When a button is pressed, all its connected universes change instantly and simultaneously.
Input
The first line contains the integer , the number of tests (). Each test follows.
Each test begins with a line containing two integers and , the number of buttons and the number of universes ().
The second line of the test contains the initial day of the week in each universe. The universes are numbered from 1 through , and the days are written in the same order.
Then each button is described on its own line. The first number is the number of universes connected to the button (), followed by the numbers of those universes. The defined numbers are different for each button.
The total number of buttons over all tests is at most 500, and the total number of universes over all tests is at most 500.
Output
For each test, print one answer on a separate line.
If all possible configurations of days of the week can be obtained, print NO. Otherwise print YES, followed by any unachievable configuration, with the days separated by spaces.