Hooking Up Power to the LEDs
Time limit1sMemory limit128 MB
Assign a voltage to each junction so every wire joins equal voltages and each LED drops between m and M, with the smallest possible highest voltage.
- Level
Medium7 of 10
- Topics
- Shortest path, Union-find, Binary search
- Solved
- No attempts yet
Problem
You are in charge of the ACM ICPC closing ceremony. To decorate the auditorium, you hired a sentimental perfectionist designer.
A few hours later, a few hours before the ceremony starts, the designer tells you the decoration is done. What waits for you in the auditorium is a strangely shaped circuit. The designer starts explaining the meaning of life and the ACM ICPC hidden all over the circuit, and takes pride in how well the circuit works. You say "if this decoration does not work, the ceremony is completely ruined", and start looking for the power supply.
You: Where is the power supply?
Designer: Sorry? That is not my part! You are the one who has to supply the power. And never touch the circuit! Just connect power to the junctions. I have to report this perfect work to my manager. Goodbye.

The structure of an LED: anode and cathode
You examine the circuit that was left behind.
An ordinary bulb glows whichever way the polarity goes, but an LED glows only when the polarity is right, that is, only when the anode pin sits at a higher voltage than the cathode pin. An LED has a minimum voltage, so even with the right direction it stays dark when the voltage across it is below that minimum. It also has a maximum voltage, and when the voltage across it goes above that maximum the part burns out.
Checking the circuit, you find that it is built from three kinds of parts.
- LED: every LED in the circuit is the same type, so they share the same minimum voltage and maximum voltage.
- Junction: the two pins of an LED connect to junctions. A junction connects wire endpoints as well as LED pins.
- Wire: every wire has two endpoints, and each endpoint connects to a junction and carries voltage across.
Connecting external terminals at different voltages to the junctions supplies voltage to the circuit. Watch out for short circuits: the two endpoints of every wire must sit at the same voltage. Electromagnetics usually takes the lowest potential as 0, so you may treat every voltage as non-negative.
Now you have to buy a power supply that fits the circuit. Its price is proportional to the maximum voltage it can supply.
Given the description of an LED circuit, write a program that decides whether every LED can be lit without a short circuit and without burning an LED. When it can be done, the program also has to report the smallest maximum voltage that lights every LED.
Input
The input has several test cases. The first line of each test case has five integers , , , , separated by spaces. is the number of junctions (), is the number of LEDs (), is the number of wires (), and and are the minimum voltage and the maximum voltage of an LED (). The junctions are numbered from to .
Each of the next lines has two integers separated by a space. The first is the number of the junction the anode pin of an LED connects to, and the second is the number of the junction its cathode pin connects to.
Each of the following lines has the numbers of the two junctions a wire joins, as two integers.
The input ends when 0 0 0 0 0 is read.
Output
Print one line for each test case.
If there is no way to light every LED, print Impossible. Otherwise print one integer, the smallest maximum voltage that lights every LED.
Hint
Potential difference and voltage mean the same thing in this problem.