Given a directed graph with vertices and arcs, and two vertices and , is there a path that goes from to , passing through at least some other vertex at least twice? We will call this a looping path. Note that it can visit and only once (at the beginning and at the end).
Input consists of several cases, each with and , followed by pairs , with , indicating an arc from to , followed by and , with . Assume , , that vertices are numbered from 0 to , and that there are no repeated arcs.
For every graph, print “YES” if there is a looping path
from
to
,
and “NO” otherwise.
Input
3 3 0 2 2 0 2 1 0 1 4 3 1 3 0 1 3 1 0 2 4 4 3 2 1 3 0 1 3 1 0 2 4 4 1 2 1 3 0 1 3 1 0 2
Output
NO NO YES YES