Loading...
Loading...
Loading...
There are n offices and m cables, each cable "a b" linking offices a and b in both directions. Then q questions follow. For each question "x y", print YES if office x can reach office y through cables (possibly through other offices), otherwise NO.
Example
Input
5 3 1 2 2 3 4 5 2 1 3 3 4
Output
YES NO
Office 1 reaches 3 through 2, but 3 cannot reach 4.
Try the problem first. Open one hint at a time.
Process all cables first with Union-Find.
Two offices are linked exactly when find returns the same leader for both.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor