#K93357. Path Existence in an Undirected Graph
Path Existence in an Undirected Graph
Path Existence in an Undirected Graph
In this problem, you are given an undirected graph (G = (V, E)) where (|V| = n) is the number of junctions (nodes) and (|E| = m) is the number of roads (edges). Each road connects two junctions. Your task is to determine whether there exists a path between a given starting junction and an ending junction.
More formally, given two nodes (s) (start) and (t) (end), check if there exists a sequence of nodes (s = v_1, v_2, \dots, v_k = t) such that each consecutive pair ((v_i, v_{i+1})) is connected by a road. If such a path exists, output YES
; otherwise, output NO
.
inputFormat
The input is read from standard input (stdin) and has the following format:
(T) – the number of test cases.
For each test case:
1. The first line contains two integers (n) and (m), representing the number of junctions and roads, respectively.
2. The next (m) lines each contain two integers (a) and (b), indicating that there is a road between junctions (a) and (b).
3. The final line of the test case contains two integers (s) and (t), the starting and ending junctions respectively.
Note: The junctions are numbered from 1 to (n).
outputFormat
For each test case, output a single line with either YES
if there is a path between the given start and end junctions, or NO
if there is no such path. The output is written to standard output (stdout).## sample
1
3 3
1 2
2 3
3 1
1 3
YES
</p>