## Description

An ascending sequence of different values refers to a sequence in which the elements increase from left to right, for example, an ordered sequence A , B , C , D A,B,C,D A. B, C, d represent A < B , B < C , C < D A<B,B<C,C<D A<B,B<C,C<D. In this problem, we will give you a series of forms such as A < B A<B A < B, and ask you to judge whether you can determine the order of this sequence according to these relationships.

## Input

The first line has two positive integers n , m n,m n,m， n n n indicates the number of elements to be sorted, 2 ≤ n ≤ 26 2\leq n\leq 26 2 ≤ n ≤ 26, th 1 1 1 to n n n elements will be capitalized A , B , C , D ... A,B,C,D\dots A. B, C, D. m m m represents the shape to be given, such as A < B A<B The number of relationships with a < B.

Next there are m m m rows, each row has 3 3 Three characters, one capital letter, one < symbol and one capital letter respectively, represent the relationship between the two elements.

## Output

If according to the previous x x This can be determined by x relationships n n The order of n elements yyy..y (such as ABC), output

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

If according to the previous x x x relationships are found to be contradictory (e.g A < B , B < C , C < A A<B,B<C,C<A A < B, B < C, C < a), output

Inconsistency found after x relations.

If according to this m m m relationships cannot be determined n n Order of n elements, output

Sorted sequence cannot be determined.

(prompt: OK) n n The program can be ended after the sequence of n elements, without considering the contradiction after determining the sequence)

## Solution

### 1. Define the judgment conditions

According to the meaning of the topic,

If the order cannot be determined, the priority is the lowest (at least m relationships must be traversed), and it is considered finally;

If there is a contradiction relationship, it is not difficult to see that there is a ring. In this case, the elements in the ring will not be traversed. At this time, the length of topological sorting is not equal to the total number of elements given. Store the topological sorting results in vector < int > ans, and determine that there is a contradiction, that is, ans.size()= n；

If ans.size() == n, that is, the topological sort exists, you also need to judge whether the topological sort is unique,

Determination method: determine whether there is a certain time, and the length of the queue used for topology sorting is not 1. If it is not 1, it proves that there are multiple points with a penetration of 0 for optional traversal, so the topology sorting is not unique.

If it is unique, output the answer directly.

### 2. Overall thinking

Read in the relationship and perform a topological sorting at the same time, so as to find the x value of the unique topological sorting or the existence of a ring when traversing the first X relationships.

### 3. Complexity estimation

Time complexity:

Topological sorting O ( n + m ) O(n+m) O(n+m), traversing m times, total time complexity O ( m ( n + m ) ) O(m(n+m)) O(m(n+m))

Space complexity:

Opened an adjacency matrix, O ( n 2 ) O(n^2) O(n2)

## Code

#include <bits/stdc++.h> #define MOD 1000000007 using namespace std; int n, m; string s; vector<int> graph[105]; //adjacency matrix int ind[30]; //Entry meter vector<int> ans; //Results after topology int topo() { ans.clear(); bool flag = false; // Copy into the scale int rudu[30] = {0}; for (int i = 1; i <= 26; i++) rudu[i] = ind[i]; queue<int> q; //Push the starting point into the queue for (int i = 1; i <= n; i++) { if (rudu[i] == 0) q.push(i); } while (!q.empty()) { // There are multiple points with a penetration of 0 at the same time, resulting in non unique topological sorting // 0 cannot be returned directly here because it has not been determined whether there is a topological sort if (q.size() > 1) flag = true; int u = q.front(); q.pop(); //Press the queue header element into the answer ans.push_back(u); //Find another point of the directed edge and put it into the degree - 1 (delete the directed edge). If the degree of the point is 0, it will be pressed into the queue for (int v = 0; v < graph[u].size(); v++) { int point = graph[u][v]; rudu[point]--; if (rudu[point] == 0) q.push(point); } } // There is a ring, some points are not traversed if (ans.size() != n) return -1; // Topology sort is not unique if (flag) return 0; // Topology sort unique return 1; } void solve() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> s; int u = s[0] - 'A' + 1, v = s[2] - 'A' + 1; graph[u].push_back(v); ind[v]++; int judge = topo(); // Existential ring (including self ring) if (judge == -1) { cout << "Inconsistency found after " << i << " relations."; return; } // Can determine the unique order else if (judge == 1) { cout << "Sorted sequence determined after " << i << " relations: "; for (int i = 0; i < ans.size(); i++) { char tmp = ans[i] + 'A' - 1; cout << tmp; } cout << "."; return; } } // Cannot determine unique order cout << "Sorted sequence cannot be determined."; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }