Loading...
Loading...
Loading...
Cables are laid one at a time between n offices, in the order given. Print the number (starting at 1) of the first cable whose two offices were already connected by the cables before it. A cable from an office to itself also counts. Print 0 if no cable makes a loop.
Example 1
Input
4 4 1 2 2 3 3 4 4 2
Output
4
Cable 4 joins offices 4 and 2, already linked through 3.
Example 2
Input
3 2 1 2 2 3
Output
0
Try the problem first. Open one hint at a time.
Treat each cable as a union of its two offices.
The first union that finds the same leader on both sides is the answer.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor