#P3858. Winning Initial Position in a Directed Graph

    ID: 17106 Type: Default 1000ms 256MiB

Winning Initial Position in a Directed Graph

Winning Initial Position in a Directed Graph

JiaJia and JinMing are two clever children who always choose the best strategy for themselves when playing games. In a modified snake game, the initial position of the snake is chosen by JiaJia. Now she wonders if she can choose an initial vertex in a directed graph so that, regardless of JinMing's moves and when both play optimally, she is guaranteed to win.

The game is defined on a directed acyclic graph (DAG) with \( N \) vertices and \( M \) directed edges. Once the starting vertex is chosen, the players take turns moving the snake along one of the outgoing edges from the current vertex. The player who is unable to move (i.e. reaches a vertex with no outgoing edges) loses the game.

A vertex \( v \) is considered winning if there exists an edge from \( v \) to some vertex \( u \) that is losing. In other words, the state can be defined by the recurrence:

$$ dp(v) = \bigvee_{u:(v\to u)} \lnot dp(u) $$

Your task is to determine whether there exists an initial vertex from which JiaJia, the first player, has a winning strategy. If such a vertex exists, output Yes; otherwise, output No.

inputFormat

The first line contains two integers \( N \) and \( M \), representing the number of vertices and the number of directed edges in the graph.

Each of the next \( M \) lines contains two integers \( u \) and \( v \), representing a directed edge from vertex \( u \) to vertex \( v \). Vertices are numbered from 1 to \( N \).

outputFormat

If there exists an initial vertex from which JiaJia can force a win, output Yes. Otherwise, output No.

sample

3 2
1 2
2 3
Yes