Dsa
Graphs Dfs
Medium
The Alternating Trail
A mountain hiker is navigating trails between peaks. Trails are either 'Smooth' (Type 0) or 'Rough' (Type 1). The hiker's knees are weak, so they cannot traverse two 'Rough' trails consecutively. Given a map of peaks and trails, determine if there is a path from the start peak to the destination peak that obeys this rule.
Input Format:
- Line 1: N peaks, M trails, Start, Destination.
- Next M lines: U, V, T (a trail between U and V of type T, undirected).
Output Format:
- 'POSSIBLE' or 'IMPOSSIBLE'.
Example:
Input:
4 4 0 3
0 1 1
1 2 1
2 3 0
0 2 0
Output:
POSSIBLE
(Explanation: 0-1-2-3 is invalid because 0-1 and 1-2 are both Rough. But 0-2-3 is Smooth-Smooth, which is valid.)
Key concepts
graphs_dfspath_constraintscycle_detection
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now