Barking Dogs!
InterviewTime limit2sMemory limit512 MB
Given dogs with wake-up delays and a directed hearing graph, simulate who barks each second from 0 to T and count each dog's barks.
- Level
Medium5 of 10
- Topics
- Simulation, Graph, Implementation, Queue
- Solved
- No attempts yet
Problem
You live in a neighbourhood full of dogs. Dogs like dogs, and they like barking even more — but best of all, dogs love to bark when other dogs bark.
Each dog has a set of dogs that can hear it bark. Each dog also has a delay: the time it waits before barking after it hears another dog bark.
Dog always barks first, and this first bark happens during second .
Assume that sound travels instantly from one dog's mouth to another dog's ear. Your job is to determine how many times each dog barks during seconds through inclusive.
In any given second, each dog is doing exactly one of three things: sleeping, waiting, or barking. If dog hears a bark during a second while it is sleeping, it wakes up, waits during seconds through inclusive, barks during second , and then goes back to sleep from second onward. If a dog hears a bark during a second in which it is waiting or barking, it ignores that bark.
During second , every dog except Dog is sleeping.
Input
The first line contains (), the number of dogs in the neighbourhood.
Each of the next lines contains an integer (), the time in seconds that dog waits before barking after it hears a bark.
The next line contains (). Each of the next lines contains two integers and , meaning that when dog barks, dog hears it. It is never the case that .
The last line contains the integer (), the number of seconds during which you monitor the dogs.
Output
Print one line for each dog, in order from dog to dog . On line , print the number of seconds with during which dog barked.