This page is still under construction.

Parts of this page are still being built. What you see may change.

Gossiping

Time limit1sMemory limit128 MB

Summary
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 nn lines (0<n<200 < n < 20), dd drivers and dd buses (0<d<300 < d < 30) numbered 11 through dd, and ss stops (0<s<500 < s < 50) numbered 11 through ss. 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 nn, dd, and ss separated by single spaces. The next 2n2n lines describe the nn 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 si dis_i\ d_i, where sis_i is the stop at which a bus starts and did_i 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.

Examples1

  1. Example 1

    Input
    2 3 5
    1 2 3
    1 1 2 2
    2 3 4 5
    2 3
    0 0 0
    
    Expected output
    Yes