Poj 1094 sorting it all out (topological sorting)

Source: Internet
Author: User

Link:

Http://poj.org/problem? Id = 1094

Question:

Sorting it all out
Time limit:1000 ms   Memory limit:10000 K
Total submissions:21532   Accepted:7403

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
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.

Analysis:

This topic is a typical topological sorting, but it changes a bit.

The three outputs of the example are as follows:

1. The order can be unique in the X-th relationship and output.

2. The inconsisitency conflict is found in the X link)

3. None of the links found the above two cases. The output is 3rd.


For a given M relationship, each read is performed in a topological order. If the first and second conditions are found, then you don't have to consider the subsequent relationships, but you still have to read them (but you don't want to deal with them ). If all the relationships have been read, and there are no situations 1 and 2, the output is 3.

There are two ways to sort topology. One is the introduction to algorithms, and the other is the greedy idea, which is better done with greedy ideas.


Greedy practice:

1. Locate all the points with 0 inbound and add them to the queue Q

2. take out a vertex of Q in the queue, and subtract 1 from the inbound degree of all its endpoints starting from this vertex. if this process finds that the inbound degree is changed to 0 after the subtraction of 1, add this vertex to the queue Q.

3. Repeat Step 2 until Q is empty.


In this process, if the inbound degree of multiple points is 0 at the same time, it indicates that the relationship cannot be uniquely determined.

If the number of sorted points after the end is less than the total number of points, there is a loop.



Note: If the side (u, v) has already been input, this side will not be added.



Code:

# Include <cstdio> # include <cstring> # include <vector> # include <queue> using namespace STD; const int n = 105; int n, m, in [N], temp [N], sort [N], T, POs, num; char X, O, Y; vector <int> G [N]; queue <int> q; void Init () {memset (in, 0, sizeof (in); For (INT I = 0; I <= N; ++ I) {G [I]. clear () ;}} inline bool find (int u, int v) {for (INT I = 0; I <G [u]. size (); ++ I) if (G [u] [I] = V) return true; return false;} int toposort () {While (! Q. empty () Q. pop (); For (INT I = 0; I <n; ++ I) if (in [I] = 0) {q. push (I);} Pos = 0; bool unsure = false; while (! Q. empty () {If (Q. size ()> 1) unsure = true; int T = Q. front (); q. pop (); sort [POS ++] = t; for (INT I = 0; I <G [T]. size (); ++ I) {If (-- in [G [T] [I] = 0) Q. push (G [T] [I]) ;}} if (Pos <n) return 1; if (Unsure) return 2; return 3 ;}int main () {int x, y, I, flag, OK, stop; while (~ Scanf ("% d % * C", & N, & M) {If (! N |! M) break; Init (); flag = 2; OK = false; for (I = 1; I <= m; ++ I) {scanf ("% C % * C", & X, & O, & Y); If (OK) continue; // if it has been determined that there is a loop or it can be uniquely sorted, do not process it, but continue to read X = x-'A', y = Y-'A '; if (O = '<'&&! Find (Y, x) {G [Y]. push_back (x); ++ in [X];} else if (O = '> '&&! Find (x, y) {G [X]. push_back (y); ++ in [y];} // copy a copy and use it to restore the in array memcpy (temp, In, sizeof (in )); flag = toposort (); memcpy (in, temp, sizeof (temp); If (flag! = 2) {stop = I; OK = true ;}} if (flag = 3) {printf ("sorted sequence determined after % d relations:", stop ); for (INT I = pos-1; I> = 0; -- I) printf ("% C", sort [I] + 'A'); printf (". \ n ");} else if (flag = 1) {printf (" inconsistency found after % d relations. \ n ", stop);} else {printf (" sorted sequence cannot be determined. \ n ") ;}} return 0 ;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.