#C8906. Fully Connected Networks
Fully Connected Networks
Fully Connected Networks
You are given several datasets describing a network of computers. Each dataset consists of a number of computers and a set of communication cables connecting pairs of computers. Your task is to determine whether the network is fully connected, meaning every computer in the network can reach every other computer through the given cables.
The network is modeled as an undirected graph. The graph is fully connected if there exists a path between every pair of nodes. In case of a single computer, the network is trivially fully connected.
Mathematically, if we denote the network as \(G=(V,E)\), where \(V\) is the set of nodes with \(|V|=n\), and \(E\) is the set of edges, the network is fully connected if and only if there is only one connected component in \(G\). That is, starting from any node, we can visit all \(n\) nodes.
inputFormat
The input consists of multiple datasets. Each dataset begins with a line containing two integers \(n\) and \(m\) where \(n\) (\(n\ge 1\)) is the number of computers and \(m\) is the number of communication cables. Each of the following \(m\) lines contains two integers, representing a communication cable between two computers. The computers are numbered from 1 to \(n\). A dataset with \(n=0\) and \(m=0\) indicates the end of input and should not be processed.
Input is given via standard input (stdin).
outputFormat
For each dataset, output a single line containing "YES" if the network is fully connected and "NO" if it is not.
Output to standard output (stdout).
3 3
1 2
2 3
1 3
4 2
1 2
3 4
5 4
1 2
2 3
3 4
4 5
0 0
YES
NO
YES
</p>