#P1347. 排序

    ID: 344 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>图论福建省历届夏令营排序拓扑排序

排序

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,DA, B, C, D means A<BA < B, B<CB < C, C<DC < D. In this problem, you are given a series of relations of the form A<BA < 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 nn and mm. Here, nn is the number of elements to sort, 2n262 \leq n \leq 26. The 11-st to the nn-th elements are represented by uppercase letters A,B,C,D,A, B, C, D, \dots. The integer mm is the number of relations of the form A<BA < B.

The next mm lines each contain exactly 33 characters: an uppercase letter, the character <, and another uppercase letter, representing a relation between two elements.

Output Format

If the first xx relations are sufficient to determine the order of the nn elements yyy...y (e.g., ABC), output

Sorted sequence determined after x relations: yyy...y.

Here, xx means the number of relations considered (the first xx relations).

If a contradiction is detected within the first xx relations (e.g., A<BA < B, B<CB < C, C<AC < A), output

Inconsistency found after x relations.

Here, xx has the same meaning as above.

If the order of the nn elements cannot be determined after all mm relations, output

Sorted sequence cannot be determined.

(Hint: Once the order of the nn 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

4 6
A<B
A<C
B<C
C<D
B<D
A<B

Sorted sequence determined after 4 relations: ABCD.
3 2
A<B
B<A
Inconsistency found after 2 relations.
26 1
A<Z
Sorted sequence cannot be determined.

Hint

2n26,1m6002 \leq n \leq 26, 1 \leq m \leq 600.

Translated by ChatGPT 5