Giant Penguin
Time limit3sMemory limit512 MB
Given a connected graph where each vertex lies on at most k simple cycles, support marking vertices and querying the distance from a vertex to the nearest marked vertex.
- Level
Hard9 of 10
- Topics
- Graph, Tree, Divide and conquer, Shortest path
- Solved
- No attempts yet
Problem
Pengsoo is a well-known giant Korean penguin. He is very rude and loves singing.
This time, Pengsoo is performing graph queries.
He has a connected undirected graph. Each vertex lies on at most vertex-simple cycles.
He wants to answer two types of queries.
- Mark a vertex .
- Find the distance from vertex to the closest marked vertex. At least one vertex is marked whenever this query is asked.
Pengsoo is very lazy and has decided to take a nap, so he asks you to perform these queries. If you do not finish before he wakes up, he will bully you, so act quickly!
Input
The first line contains three integers , , and (, , ): the number of vertices, the number of edges, and the maximum number of vertex-simple cycles that pass through one vertex.
The next lines each contain two integers and (, ), denoting an edge between and . There are no multiple edges, the graph is connected, and each vertex lies on at most vertex-simple cycles.
The next line contains one integer (): the number of queries.
The next lines each contain two integers and (, ). If , mark vertex . Vertex was not marked before. If , print the distance from to the closest marked vertex. At least one vertex is marked at that time.
Output
For each query with , print the distance to the closest marked vertex.