#C11503. Animal Interaction Path

    ID: 40827 Type: Default 1000ms 256MiB

Animal Interaction Path

Animal Interaction Path

You are given n animals labeled from 1 to n and m interactions between pairs of animals. Each interaction represents a bidirectional connection between two animals. Your task is to determine if there exists a sequence of interactions connecting animal X to animal Y. In other words, check if there is a path in the undirected graph where vertices represent animals and edges represent interactions.

The problem can be mathematically described as follows:

Given a graph \(G=(V,E)\) where \(V = \{1, 2, \dots, n\}\) and \(E\) is the set of interactions, determine if there exists a path from node \(X\) to node \(Y\).

If \(X = Y\), you should output "YES".

inputFormat

The input is given via standard input (stdin) and has the following format:

N M
u1 v1
u2 v2
... 
uM vM
X Y

Where:

  • N is the number of animals.
  • M is the number of interactions.
  • Each of the next M lines contains two integers u and v, indicating an interaction between animals u and v.
  • The last line contains two integers X and Y denoting the animals between which you need to determine if a path exists.

outputFormat

The output should be written to standard output (stdout) and consist of a single line containing either "YES" if a path exists from X to Y, or "NO" otherwise.

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