Driver Disagreement
Time limit2sMemory limit512 MB
Given a map where each intersection has left and right exits and a tower flag, find the fewest shared turns from A and B that expose different tower visibility.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Union-find
- Solved
- No attempts yet
Statement
Alice and Bob are travelling in Italy by car, and they took a wrong turn. Now they are stuck in the city centre of Pisa. You need an allowance to drive in the city centre, so they risk getting a fine. They have a map but no GPS. The map lists all intersections. At each intersection you can turn either left or right. You cannot go straight or make a U-turn, because many streets are one-way.
Alice thinks they are at intersection , while Bob thinks they are at intersection . They want to find out who is right. They plan an experiment: they trace the same sequence of turns on the map, once starting from and once starting from . The map marks the intersections from which the leaning tower of Pisa is visible. As soon as the tower should be visible for one of them but not for the other, they look out of the window and see who is right. You may assume exactly one of them is right.
Find the minimal number of turns needed to show who is right.
Input
The first line contains three integers , , and , where , , and .
Then follow lines. The -th of these lines contains three integers , , and . At intersection , a left turn leads to and a right turn leads to . if the leaning tower of Pisa is visible from intersection , and otherwise.
Output
Print the minimal number of turns it takes to show either person correct. If no experiment can tell whether Alice or Bob is correct, print indistinguishable.