P1347 Sorting
Description
An ascending sorted sequence of distinct values is a sequence whose elements strictly increase from left to right. For example, an ordered sequence $A, B, C, D$ means $A < B$, $B < C$, $C < D$. In this problem, you are given a series of relations of the form $A < B$, and you are asked to determine whether these relations are sufficient to determine the order of the sequence.
Input Format
The first line contains two positive integers $n$ and $m$. Here, $n$ is the number of elements to sort, $2 \leq n \leq 26$. The $1$-st to the $n$-th elements are represented by uppercase letters $A, B, C, D, \dots$. The integer $m$ is the number of relations of the form $A < B$.
The next $m$ lines each contain exactly $3$ characters: an uppercase letter, the character `
Output Format
If the first $x$ relations are sufficient to determine the order of the $n$ elements `yyy...y` (e.g., `ABC`), output
`Sorted sequence determined after x relations: yyy...y.`
Here, $x$ means the number of relations considered (the first $x$ relations).
If a contradiction is detected within the first $x$ relations (e.g., $A < B$, $B < C$, $C < A$), output
`Inconsistency found after x relations.`
Here, $x$ has the same meaning as above.
If the order of the $n$ elements cannot be determined after all $m$ relations, output
`Sorted sequence cannot be determined.`
(Hint: Once the order of the $n$ elements is determined, you can terminate the program immediately. You do not need to consider contradictions that may appear after that.)
# Input Format
# Output Format
Explanation/Hint
$2 \leq n \leq 26, 1 \leq m \leq 600$.
Translated by ChatGPT 5