#P1347. Determine Unique Ascending Order
Determine Unique Ascending Order
Determine Unique Ascending Order
This problem asks you to determine whether a sequence can be uniquely determined given a set of relations. A strictly increasing sequence of distinct values is such that the elements increase from left to right, i.e. if the sequence is A, B, C, D, then it satisfies \(A<B, B<C, C<D\).
You are given several relations in the format \(A<B\). Based on these relations, determine if there exists a unique ordering of the elements that is consistent with the relations provided. If the ordering is uniquely determined, output YES
; otherwise, output NO
. Note that if the relations are contradictory (i.e. they cannot all be satisfied), the sequence is not uniquely determined.
inputFormat
The input begins with a line containing two integers \(n\) and \(m\) where \(n\) is the number of distinct elements (represented by uppercase letters starting from 'A') and \(m\) is the number of relations. The following \(m\) lines each contain a relation in the form A<B
(without quotes), indicating that the element A is less than the element B.
outputFormat
Output a single line containing YES
if the given relations uniquely determine the sorted order of the \(n\) elements, otherwise output NO
.
sample
4 3
A<B
B<C
C<D
YES