Dsa
Graphs Dfs
Hard
Cyclic Cities Exploration
You are given a directed graph representing a set of cities (nodes) and flights (edges). Your task is to determine whether there is a cycle in the graph. If there is a cycle, you should return 'Yes', otherwise return 'No'.
Input Format:
- The first line contains an integer n, the number of cities.
- The next lines describe edges in the form of 'u -> v', where u and v are city indices starting from 0, representing a flight from city u to city v. The input ends when a line containing 'END' is encountered.
Output Format:
- Print 'Yes' if there is a cycle in the graph, otherwise print 'No'.
Example Input:
4
0 1
1 2
2 0
3 1
END
Example Output:
Yes
Key concepts
graphsdfscycle 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