Bus Riding
Time limit2sMemory limit1024 MB
Simulate people riding circular bus routes, tracking boarding order by smallest route number and each transfer's one-minute wait, to report the finish time and stop or 0 0.
- Level
Hard8 of 10
- Topics
- Simulation, Implementation, Math, Array
- Solved
- No attempts yet
Problem
A city has bus stops, and circular bus routes pass through them. Each route is given as a list of the numbers of the stops it passes through, and the -th route passes through stops in that order. Exactly one bus runs on each route. At time 0 this bus is at stop . The bus takes exactly one minute to reach the next stop on its route. The time the bus spends standing at a stop can be ignored. All routes are circular, meaning that one minute after leaving stop the bus arrives at stop and travels the route again.
Several people in this city decided to ride the buses. Each of them made a plan for their ride. The plan of the -th person consists of the stop where the person starts riding and a sequence of numbers . These numbers mean the following: at time 0 the person comes to stop and waits for the nearest bus (if at that moment some bus is at stop , the person boards it). On this bus they ride past stops, then get off and wait for the next bus at the stop where they ended up. On that bus they ride past stops, get off again, and wait for the next bus again. And so on. If at some moment several buses arrive at a stop at once, the person boards the bus with the smallest route number. When a person gets off a bus at some stop, they can leave that stop no earlier than one minute later.
For each person, determine how many minutes after the starting moment their ride ends and at which stop it ends.
Input
The input file first contains the number , then the number . Then lines follow, specifying the bus routes. Each line begins with the number , which gives the length of the route, followed by the list of stops the route passes through: . A route may pass through the same stop several times.
Then comes the number , the number of people, followed by lines specifying the people's plans. Each line contains first the number , the starting stop number, and , the number of numbers in the sequence. Then come the numbers .
All numbers in the input file are positive integers not exceeding 50.
Output
For each person, output two numbers to the output file: the time in minutes when their ride ends, and the number of the stop where this happens. If the person cannot complete their plan (at some stop they will not wait for a bus), output two zeros for them.