Dsa
Union Find
Easy
Detect Cycles in a Network
You are given a list of connections representing direct links between computers in a network. Some connections may be redundant. Your task is to determine if any of the connections create a cycle in the network. A cycle exists if there is a path that starts and ends at the same computer without retracing any connection.
Input Format:
- The first line contains an integer N (1 ≤ N ≤ 1000), the number of connections.
- Each of the next N lines contains two integers a and b (1 ≤ a, b ≤ 1000) indicating a direct connection between computer a and computer b.
Output Format:
- Print "YES" if there is a cycle in the network, otherwise print "NO".
Example Input:
3
1 2
2 3
3 1
Example Output:
YES
Key concepts
union_findgraph
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