Catching the Joker
InterviewTime limit2sMemory limit1024 MB
Given a tree and m paths, find the vertex from which the most paths can be walked as one simple route through every vertex on the path.
- Level
Medium6 of 10
- Topics
- Tree, DFS, Prefix sum
- Solved
- No attempts yet
Problem
The Joker is plotting something again. Batman plans to find him and stop him.
Gotham consists of intersections connected by two-way roads, so that there is exactly one path along the roads between any two intersections.
Batman knows that the Joker will soon travel from his hideout to a secret base. He does not know exactly where these two places are. He has guesses, and the -th guess is that the Joker travels from intersection to intersection .
If Batman stands at intersection , he can catch the Joker traveling between and if, starting from , he can fly over the roads so that he passes through every intersection on the path from to without flying over any road twice.
Batman wants to occupy an intersection so that he can catch the Joker on as many of the guessed routes as possible. Count how many routes he can catch the Joker on if he picks the best intersection.
Input
The first line contains the number of intersections in Gotham ().
The next lines describe the roads. Each road is given by two numbers and , the intersections it connects (, ). It is guaranteed that there is exactly one path between any two intersections.
The next line contains the number of guessed routes ().
The next lines describe the routes. The -th line gives the start and the end of the -th route (, ). Routes may overlap or coincide.
Intersections are numbered starting from 1.
Output
Print one integer: the maximum number of guessed routes on which Batman can catch the Joker if he occupies the best intersection.