Recursive circuit
Time limit1sMemory limit256 MB
Given a self-similar circuit graph, find for each query the smallest critical nesting depth needed to connect two input contacts, or -1 if they cannot be connected.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Sorting
- Solved
- No attempts yet
Problem
In the layout of a recursive microcircuit, there are contacts in total, and some pairs of contacts are connected directly by conductive paths. The circuit also has sub-circuits, each an exact copy of this circuit.
The layout has three types of contacts:
- Input contacts of the circuit ( contacts). These are the only contacts that connect the circuit to external conductive paths.
- Input contacts of the nested sub-circuits ( contacts).
- Auxiliary contacts.
All these contacts can be connected with each other by conductive paths without any restrictions.
Signals travel along conductive paths. When a signal reaches a contact, it can follow any path connected to that contact. If an external signal reaches an input contact of a sub-circuit, it can enter the sub-circuit and travel further along its paths. If an internal signal reaches an input contact of a sub-circuit, it can leave the sub-circuit and travel outside the circuit (if there are paths outside, and if the external circuit is itself a sub-circuit of another circuit).
Consider the outermost circuit, with nothing outside it. Determine whether two specified input contacts of the circuit are connected by paths. Two contacts are connected if a signal can reach one from the other, possibly entering various sub-circuits a finite number of times.
Besides connectivity, find how deep the signal must go into the sub-circuits to reach one contact from the other. The outermost circuit has nesting depth 0. Its sub-circuits have depth 1, their sub-circuits have depth 2, and so on. For any signal path, the critical depth is the maximum nesting level among all circuits whose conductive paths the signal travels. Find the minimal critical depth over the paths between two given input contacts of the outermost circuit.
Input
The first line contains four integers: (the number of contacts in the layout), (the number of input contacts of the circuit), (the number of sub-circuits), and (the number of conductive paths) (, , , ).
Each of the following lines defines a conductive path by two integers and , the numbers of the contacts it connects directly ().
Contacts are numbered from to . Input contacts of the circuit are numbered from to . Input contacts of the -th sub-circuit are numbered from to (for ). The -th input contact of the -th sub-circuit is the -th contact of the external circuit layout. The remaining contacts, if any, are auxiliary.
The next line contains , the number of queries (). Each of the following lines contains two integers and , the numbers of two input contacts of the outermost circuit ().
Output
Print integers, one per line. The -th number is the nesting depth needed to get from one of the two input contacts of the -th query to the other. If the other input contact cannot be reached, print instead.
Hint
