Multithreaded Program
InterviewTime limit2sMemory limit512 MB
Given per-thread assignment sequences and final variable values, decide whether a valid interleaving exists and, if so, output one execution.
- Level
Medium6 of 10
- Topics
- Greedy, Topological sort
- Solved
- No attempts yet
Problem
Maurice is debugging a multithreaded program on his old machine. The program has several threads operating on a set of shared variables. Each thread executes its own sequence of assignments in a predefined program order. Each assignment sets one variable to an integer value.
When the program runs, assignments from different threads can execute in any order. The only guarantee is that each thread executes all of its assignments in program order.
For example, suppose the program has three threads with , , and assignments in their sequences. One valid execution is:
- thread executes the first assignment in its sequence;
- thread executes the first assignment in its sequence;
- thread executes the second assignment in its sequence;
- thread executes the second assignment in its sequence;
- thread executes the only assignment in its sequence.
This execution can be described as , where the numbers give the thread that performs each assignment, in order. Many other valid executions are also possible.
Maurice suspects that his machine is broken and may work incorrectly. He ran the program and recorded the values of all variables at the end.
Find an execution that performs all assignments and leads to the recorded values of all variables, or report that the machine is broken and no such execution exists.
Input
The first line contains a single integer , the number of threads (). The threads are numbered from to . The following lines describe sequences of assignments, one per thread.
The first line of the -th description contains an integer , the length of the sequence of assignments of the -th thread (). Each of the following lines contains an assignment in the form <variable>=<value>. The assignments are listed in program order. Variable names consist of up to lowercase English letters, and values are positive integers not exceeding .
The first of the remaining lines contains an integer , the number of variables (). Each of the following lines contains a variable name and its recorded value, which is a positive integer not exceeding . Each variable used in the program is listed exactly once, and each listed variable is used in at least one assignment.
Output
Print Yes if an execution producing the recorded values exists, and No otherwise.
If an execution exists, print a line containing integers , describing such an execution (). This specifies that the first assignment is performed by thread , the second one by thread , and so on. Each thread performs its assignments in program order. After the -th assignment, each variable must have the recorded value. The -th thread must appear in the description exactly times.