SCC25 Contest 1 P4 - Mike's Maze
Mike has trouble deciding which university he wants to attend next year. As a result he puts representatives from each university into a maze and decide that the first one out will be his choice.
Inside the maze, you decide you need to have a strategy before continuing. You recall that its a good idea to always turn right, making you guarenteed to find the exit in a conventional maze. In this maze the rightmost path is the one which leads to a node with the least amount of factors or the smaller number if they have the same amount of factors. For example given paths towards \(3\) and \(2\), the rightmost path is towards node \(2\) as both \(3\) and \(2\) have \(2\) factors and \(2\) is the smaller number. With this strategy, can you determine if you are able to escape the maze?
The maze is described by \(N\) rooms with \(M\) paths connecting them. These paths are extra dangerous, being one way slides. That means that if a path connects \(1\) and \(2\), you cannot take it to go from \(2\) to \(1\) unless you are retracing your steps, in which case you can use a rope to pull you up.
It is possible that Mike wants to kill all the current execs and has not made an exit to the maze!
The starting room will always be room \(1\) and the exit room will always be room \(N\)
Constraints
\(1 \leq N, M \leq 5\times10^5\)
\(1 \leq u_i, v_i \leq N\)
Input Specification
The first line contains two integers, \(N\) and \(M\).
The next \(M\) lines contain one valid edge each.
Output Specification
Output YES
if you can exit the maze or NO
if there is no way to exit or you will get stuck.
Sample Input 1
4 4
1 2
2 3
1 3
3 4
Sample Output 1
YES
Sample Input 2
4 4
1 2
2 3
3 2
1 4
Sample Output 2
NO
Comments