Dsa
Union Find
Hard
Network Connectivity
You are given a number of computers in a network, each identified by a unique ID from 1 to n. You will receive a list of connections, where each connection is an undirected edge between two computers. Your task is to determine if all computers are connected directly or indirectly (i.e., if there exists a path between any two computers). If they are all connected, print 'YES'; otherwise, print 'NO'.
Input Format:
The first line contains two integers n (number of computers) and m (number of connections).
The next m lines contain two integers u and v, representing a connection between computer u and computer v.
Output Format:
Print 'YES' if all computers are connected, 'NO' otherwise.
Example:
Input:
5 4
1 2
2 3
3 4
4 5
Output:
YES
Key concepts
union_findgraphconnectivity
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