Dsa
Graphs Dfs
Medium
The Phase-Shift Network
A signal travels through a network of N servers (0 to N-1). Some servers are 'Phase-Inverters'. When a signal enters a Phase-Inverter server, its polarity flips (Positive becomes Negative, Negative becomes Positive). Non-inverters keep the polarity the same. Given a start server, a target server, and the initial polarity (Positive), determine if there exists a path that reaches the target with a Negative polarity. You cannot visit the same server twice with the same polarity.
Input Format:
- Line 1: N, M, Start, Target.
- Line 2: N integers (0 or 1). 1 means the server is a Phase-Inverter, 0 means it is not.
- Next M lines: U V (a directed connection from U to V).
Output Format:
- 'YES' if target is reachable with Negative polarity, else 'NO'.
Example:
Input:
3 2 0 2
0 1 0
0 1
1 2
Output:
YES
(Explanation: Path 0 (Pos) -> 1 (flips to Neg) -> 2 (stays Neg).)
Key concepts
graphs_dfsstate_space_searchreachability
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