#C550. Path Existence in a Directed Graph

    ID: 49156 Type: Default 1000ms 256MiB

Path Existence in a Directed Graph

Path Existence in a Directed Graph

Given a directed graph with n nodes and m edges, determine if there exists a path from node A to node B.

You are given the following conditions:

  • \(1 \leq n \leq 10^5\)
  • \(0 \leq m \leq 10^5\)

The next \(m\) lines describe the directed edges of the graph, where each edge is given by two integers \(u\) and \(v\), representing a directed edge from node \(u\) to node \(v\).

Your task is to check if there exists at least one path from node A to node B. Output YES if such a path exists, otherwise output NO.

inputFormat

The input is read from standard input (stdin) and has the following format:

n m A B
u1 v1
u2 v2
... 
um vm

Here:

  • n: number of nodes
  • m: number of edges
  • A: starting node
  • B: destination node
  • Each of the next m lines contains two integers u and v representing a directed edge from u to v.

outputFormat

Output a single line to standard output (stdout):

YES

if there exists a path from node A to node B, or

NO

if there is no such path.

## sample
5 4 1 5
1 2
2 3
3 4
4 5
YES