#K55522. Travel to Last Node
Travel to Last Node
Travel to Last Node
You are given a directed graph with \( n \) nodes and \( m \) edges. Your task is to determine whether there exists a path from node 1 to node \( n \). You can travel along the given directed edges. This is a classic reachability problem that can be solved using breadth-first search (BFS).
Input Constraints:
- The first line contains two integers \( n \) and \( m \).
- Each of the next \( m \) lines contains two integers \( u \) and \( v \), representing a directed edge from node \( u \) to node \( v \).
Output: Print "YES" if there is a path from node 1 to node \( n \), otherwise print "NO".
Example:
If \( n = 5 \) and \( m = 6 \) with the following edges:
1 2
2 3
3 4
4 5
1 3
2 5
The output should be "YES" because there exists a path from node 1 to node 5.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains two space-separated integers \( n \) and \( m \), representing the number of nodes and edges respectively.
- Each of the next \( m \) lines contains two space-separated integers \( u \) and \( v \), representing a directed edge from node \( u \) to node \( v \).
outputFormat
Output a single line to standard output (stdout) containing either "YES" if there is a path from node 1 to node \( n \), or "NO" otherwise.
## sample5 6
1 2
2 3
3 4
4 5
1 3
2 5
YES