Dsa
Graphs Bfs
Easy
Network Emergency Response
In a network of computers, each computer can directly communicate with others. You need to determine how many separate networks are present in the system if some computers are not directly connected to others. Write a program that reads a list of connections between computers and outputs the number of distinct networks.
Input Format:
- The first line contains an integer n (number of computers).
- The second line contains an integer m (number of connections).
- The next m lines each contain two integers a and b representing a connection between computer a and b.
Output Format:
- Print the number of distinct networks.
Example:
Input:
5
3
1 2
2 3
4 5
Output:
2
Key concepts
bfsgraphconnected-components
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