Gossiping
Time limit1sMemory limit128 MB
Given bus loops with drivers moving in lockstep, decide whether every driver eventually meets every other driver's news.
- Level
Medium7 of 10
- Topics
- Simulation, Math, Number theory, Graph
- Solved
- No attempts yet
Problem
A town runs a public bus system. Every bus line is a closed loop with at least two stops, and different lines may share stops. Several buses operate, each driven by exactly one driver. The buses move in lockstep: in one time unit every bus advances from its current stop to the next stop on its line, and after the last stop of the loop it returns to the first.
At the very start each driver knows one unique piece of news that no other driver knows. Whenever two or more drivers are at the same stop at the same moment, they tell each other everything they currently know, so afterwards all of them share the same combined set of news.
There are lines (), drivers and buses () numbered through , and stops () numbered through . Several buses may run on the same line, each starting from a possibly different stop of that line.
Decide whether every driver will, at some moment, come to know all of the news held by the others.
Input
The input consists of several blocks; every block except the last describes one town.
The first line of a block contains three integers , , and separated by single spaces. The next lines describe the bus lines, two lines per bus line:
- The first line lists the stop numbers along that line in the order the bus visits them; after the last listed stop the bus returns to the first.
- The second line describes the buses that start on this line as pairs , where is the stop at which a bus starts and is the number of its driver. All numbers on the line are separated by single spaces.
The final block is a single line containing 0 0 0 and must not be processed.
Output
For every block except the terminating one, print a single line: Yes if in that town every driver will eventually learn all the news of the others, and No otherwise. Print the answers in the same order as the blocks appear in the input.