Distance on Triangulation
Time limit2sMemory limit256 MB
Answer the shortest path length along polygon sides and diagonals for many queried vertex pairs in a triangulated convex polygon.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Shortest path, Graph, BFS
- Solved
- No attempts yet
Problem
You have a convex polygon. Its vertices are numbered from 1 to in order around the boundary. You also have a triangulation of this polygon, given as diagonals that do not cross each other.
You are given queries. Each query consists of two vertex numbers. Moving only along the sides of the polygon and along the given diagonals, find the shortest distance between the two vertices of the query. The distance is the number of sides and diagonals you traverse.
Input
The first line contains an integer , the number of vertices of the polygon ().
Each of the next lines contains two integers and , the endpoints of the -th diagonal (, ).
The next line contains an integer , the number of queries ().
Each of the next lines contains two integers and , the two vertices of the -th query ().
No diagonal coincides with a side of the polygon, and no two diagonals coincide or cross.
Output
For each query print the shortest distance on its own line.
Hint
The picture shows the polygon and the triangulation of the first example.
