Connect
Time limit2.5sMemory limit1024 MB
For each city pair, find the lowest-numbered road whose closure breaks two-way reachability in a directed graph.
- Level
Hard9 of 10
- Topics
- Graph, DFS, Divide and conquer
- Solved
- No attempts yet
Problem
Because of all the travelling around the country for IATI 2022, the tour guide company of Kyusho was once again overwhelmed with questions. The region that his company is responsible for consists of cities, numbered from to . Between them, there are direct roads, numbered from to , each connecting a pair of different cities. There may be more than one direct road in a given direction between two cities.
The dispatch department often receives questions from bus drivers of this form: "Can you always go from city to city and then go back to city even if one of the roads is closed?"
Kyusho knows that you are an experienced programmer, so he asks you to write a program connect that answers these questions.
Input
The first line contains two positive integers , the number of cities, and , the number of direct roads. Each of the next lines contains two positive integers and , meaning there is a direct road from city to city .
The next line contains the positive integer , the number of questions. Each of the following lines contains two positive integers and , describing one question about cities and .
Sometimes there are so many questions that it is better to get the answer for every unordered pair of cities . When , the answers are needed for the pairs , in that order.
Output
For each question, assign a number as follows.
- : Even without closing one of the roads, there is no route between the two cities in at least one of the two directions.
- : No matter which road is closed, there is always a route between the two cities in both directions.
- A number between and : the number of the road whose closure leaves no route in one of the two directions. If more than one road qualifies, print the lowest-numbered one.
Let the resulting numbers be , where if , and if . Print on one line the remainder of
modulo , where .