#C9467. Network Connectivity Check
Network Connectivity Check
Network Connectivity Check
You are given a network with n nodes and m edges. Each edge connects two distinct nodes. Your task is to determine whether there is a path between two specified nodes a and b in the network.
The network is undirected. Formally, given a graph \(G=(V,E)\) where \(V = \{1, 2, \dots, n\}\) and \(E\) consists of m edges, check if there exists a sequence of vertices \(v_1, v_2, \dots, v_k\) such that \(v_1 = a\), \(v_k = b\) and for every \(i=1, 2, \dots, k-1\), \((v_i, v_{i+1}) \in E\).
Output YES
if a path exists, otherwise output NO
.
inputFormat
The first line contains two integers \(n\) and \(m\) (the number of nodes and edges respectively).
The next \(m\) lines each contain two integers \(u\) and \(v\), representing an undirected edge between nodes \(u\) and \(v\).
The last line contains two integers \(a\) and \(b\): the two nodes to check connectivity.
All input is given through stdin
.
outputFormat
Output a single line: YES
if there exists a path between \(a\) and \(b\) in the network, and NO
otherwise. Write the answer to stdout
.
6 5
1 2
2 3
1 3
4 5
5 6
1 3
YES
</p>