#K48397. Connectivity Check in Graph
Connectivity Check in Graph
Connectivity Check in Graph
You are given an undirected graph with (N) nodes and (M) edges. Each edge connects two nodes bidirectionally. Your task is to determine whether there exists a path between two specified nodes, (A) and (B).
The graph is provided as a list of (M) pairs of integers, each representing an edge. A path exists if there is a sequence of nodes (v_0, v_1, \ldots, v_k) such that (v_0 = A), (v_k = B), and for every (i) with (0 \le i < k), there is an edge between (v_i) and (v_{i+1}).
You can use breadth-first search (BFS) or depth-first search (DFS) to determine the connectivity between the nodes.
inputFormat
The input is given via standard input (stdin) and has the following format:
1. The first line contains an integer (N) representing the number of nodes.
2. The second line contains an integer (M) representing the number of edges.
3. The next (M) lines each contain two space-separated integers (u) and (v) indicating there is an edge between nodes (u) and (v).
4. The last line contains two space-separated integers (A) and (B) representing the nodes between which you need to check connectivity.
outputFormat
Output a single line to standard output (stdout) with the string "YES" if there is a path from (A) to (B); otherwise, output "NO".## sample
5
5
1 2
2 3
4 5
1 3
3 4
1 5
YES