#P1346. 电车
电车
Description
In a magical small town, there is a special tram network composed of intersections and tracks. Each intersection connects to several tracks, and each track leads to an intersection (some scenic tracks may loop back to the same intersection). At every intersection, there is a switch that determines the outgoing track. Each switch has a default position. When a tram arrives at an intersection, it can only leave along the track currently selected by the switch. If the driver wants to take a different track, they must get off and toggle the switch.
To reach the destination, drivers often have to get off to toggle switches. Please write a program to compute the minimum number of switch toggles needed for a tram to travel from intersection to intersection .
Input Format
The first line contains 3 integers ( ), representing the number of intersections, and the start and destination intersections.
Then lines follow. Each line starts with a number ( ), indicating that this intersection is connected to tracks, followed by integers, each giving the intersection that the corresponding track leads to. The switch’s default position points to the track indicated by the first of these integers.
Output Format
Output a single integer: the minimum number of switch toggles required to go from to . If it is impossible to reach from , output .
3 2 1
2 2 3
2 3 1
2 1 2
0
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号