Ants and the Ladybug
Time limit1sMemory limit128 MB
Simulate guards moving on a tree toward successive ladybug landings, respecting blocking rules, and report each ant's final place and chase count.
- Level
Medium5 of 10
- Topics
- Tree, Simulation, Implementation, BFS
- Solved
- No attempts yet
Problem
Ants farm aphids: the aphids give off sweet honeydew that the ants drink, so the ants protect the aphids from their fiercest enemy, the ladybug. On a tree beside the anthill lives a colony of aphids that feeds on the tree's leaves and branch points.
The tree has places, its leaves and branch points, numbered from to and joined by branches, so between any two places there is exactly one simple route. The tree is patrolled by guard-ants, numbered from to ; each ant stands on a place of its own, and no place holds more than one ant.
A ladybug lands on one of the places. The ants then try to chase her away, and they all move at the same speed: crossing one branch takes one unit of time. Each landing is resolved by the following rules:
- If an ant already stands on the ladybug's place, she takes off at once, that ant is credited with chasing her away, and no ant moves for this landing.
- Otherwise every ant heads toward her along the unique route from its place to hers, but an ant sets off only if no other ant stands on that route (its own place excepted); an ant that is blocked this way does not move at all and stays where it is.
- All ants that set off advance together, one branch per unit of time.
- If two or more ants would enter the same place at the same moment, only the lowest-numbered of them enters it; each of the others stops on its place and wanders no further.
- The moment an ant reaches the ladybug's place it chases her away and stays there; at that instant every other ant also stops on the place it currently occupies.
The ladybug is stubborn and keeps returning, landing on the tree again and again. Each time she lands, the ants set off afresh from wherever they now stand.
Write a program that, given the tree, the ants' starting places, and the ladybug's successive landing places, reports for each ant its final place and how many times that ant chased the ladybug away.
Input
The first line holds one integer (), the number of places. Each of the next lines holds two integers and (): a branch joins places and .
The next line holds one integer ( and ), the number of ants. Each of the following lines holds one integer in : the starting place of the -th ant. All starting places are distinct.
The next line holds one integer (), how many times the ladybug lands. Each of the following lines holds one integer in : a landing place, given in the order the landings happen.
Output
Print lines. On the -th line print two integers separated by a single space: the final place of the -th ant and the number of times that ant chased the ladybug away.
Hint
The picture below shows the tree of the first example.

In that example the branches are , , and ; ant starts at place and ant starts at place . The ladybug first lands on place : ant is already there, so it chases her away at once and nobody moves. She then lands on place : the route from ant (place ) to place runs through place , where ant stands, so ant stays put, while ant walks from place to place in one step and chases her away again. In the end ant is still at place with chases, and ant is at place with chases.