#C3172. Hamiltonian Cycle Detection in a Directed Graph
Hamiltonian Cycle Detection in a Directed Graph
Hamiltonian Cycle Detection in a Directed Graph
You are given a directed graph with \( n \) vertices (numbered from 1 to \( n \)) and \( m \) directed edges. Your task is to determine whether the graph contains a Hamiltonian cycle. A Hamiltonian cycle is a cycle that visits each vertex exactly once and returns to the starting vertex. Formally, a cycle \( v_1, v_2, \ldots, v_n, v_1 \) is Hamiltonian if for every \( i = 1, 2, \ldots, n \), there is a directed edge from \( v_i \) to \( v_{i+1} \) (with \( v_{n+1} = v_1 \)).
If such a cycle exists, print YES
; otherwise, print NO
.
inputFormat
The input is read from standard input and has the following format:
- The first line contains two integers ( n ) and ( m ) where ( n ) is the number of vertices and ( m ) is the number of directed edges.
- Each of the next ( m ) lines contains two integers ( u ) and ( v ) representing a directed edge from vertex ( u ) to vertex ( v ).
outputFormat
Print a single line to standard output containing either "YES" if there exists a Hamiltonian cycle in the graph, or "NO" otherwise.## sample
4 5
1 2
2 3
3 4
4 1
2 4
YES