Sorting it all out
| Time limit:1000 ms |
|
Memory limit:10000 K |
| Total submissions:26866 |
|
Accepted:9267 |
Description
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. for example, the sorted sequence a, B, c, d implies that a <B, B <C and C <D. in this problem, we will give you a set of relations of the form a <B and ask you to determine whether a sorted order has been specified or not.
Input
Input consists of multiple problem instances. each instance starts with a line containing two positive integers n and M. the first value indicated the number of objects to sort, where 2 <= n <= 26. the objects to be sorted will be the first n characters of the uppercase alphabet. the second value M indicates the number of relations of the form a <B which will be given in this problem instance. next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. no letter will be outside the range of the First n letters of the alphabet. values of N = m = 0 indicate end of input.
Output
For each problem instance, output consists of one line. This line shocould be one of the following three:
Sorted sequence determined after XXX relations: YYY... y.
Sorted sequence cannot be determined.
Inconsistency found after XXX relations.
Where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and YYY... Y is the sorted, ascending sequence.
Sample Input
4 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sample output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
You need to understand the question ....
Sorted sequence determined after 4 Relations: ABCD. after the first four links are entered, a sequence is determined (the total number of links may be greater than 4) inconsistency found after 2 relations. after the first two links are entered, a conflict (loop, the total number of links may be greater than 2) Sorted sequence cannot be determined. after all link inputs, no sequence or conflict occurs.
Partial and full order:
From the perspective of Discrete Mathematics, topological sorting is to obtain a full order of a set from an partial order of a set.
Intuitively, partial order means that only some elements in the set can be compared (some elements cannot be compared), and full order means that all elements in the set can be compared.
More intuitively, a partial order can be a flow chart, indicating the order relationship between steps in the process of completing a task. The Topology Sorting task obtains a full order in this partial order, A sequence of steps to complete the entire project is obtained.
The ordering dependency principle is the priority relationship between each step.
The sequence obtained by topological sorting is not necessarily unique, because there is no priority relationship between some steps (this is the characteristic of partial order). Some rules are manually added during topological sorting, make the obtained sequence a full order that satisfies the partial order relationship.
A directed acyclic graph is more helpful for understanding.
# Include <cstdio> # include <iostream> # include <cstring> # include <queue> # include <algorithm> # include <vector> using namespace STD; const int M = 30; const int n= 1000 + 50; int n, m; char inchar [N] [m]; char outchar [m]; int in [m]; int cut; vector <int> AMAP [m]; int flag; int toposort () {cut = 0; queue <int> que; int temp [m]; memcpy (temp, In, sizeof (in); bool all_out = true; // whether it is a fully ordered mark for (INT I = 0; I <n; I ++) if (! Temp [I]) que. Push (I); While (! Que. empty () {If (que. size ()> 1) // If the queue is not an element, all_out = false; int A = que. front (); que. pop (); outchar [CUT ++] = a + 'a'; For (INT I = 0; I <AMAP [A]. size (); I ++) {int B = AMAP [a] [I]; If (-- temp [B] = 0) // If the element appears multiple times, then, do not enter que. push (B) ;}}if (cut <n) Return-1; // cut <n, indicating that a ring exists. If (all_out = true) return 1 is in conflict with else; else return 0;} int main () {While (scanf ("% d", & N, & M) = 2 & N & M) {mem Set (in, 0, sizeof (in); For (INT I = 0; I <n; I ++) AMAP [I]. clear (); For (INT I = 0; I <m; I ++) scanf ("% s", inchar [I]); flag = 0; int CAS; for (CAS = 0; CAS <m; CAS ++) {int A = inchar [CAS] [0]-'A '; int B = inchar [CAS] [2]-'A'; AMAP [A]. push_back (B); in [B] ++; flag = toposort (); If (flag! = 0) break;} outchar [N] = '\ 0'; // Add an end character if (flag = 1) After the output data) printf ("sorted sequence determined after % d relations: % S. \ n ", CAS + 1, outchar); else if (flag = 0) printf (" sorted sequence cannot be determined. \ n "); else if (flag =-1) printf (" inconsistency found after % d relations. \ n ", CAS + 1);} return 0 ;}