Poj 1094 & zoj 1060 sorting it all out [Topology Sorting entry]

Source: Internet
Author: User
Tags uppercase letter

Original question link: http://poj.org/problem? Id = 1094

Same as zoj 1060: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 1060.

Sorting it all out

Time limit:1000 ms

 

Memory limit:10000 K

Total submissions:21983

 

Accepted:7574

Description

Anascending sorted sequence of distinct values is one in which some form of aless-than operator is used to order the elements from smallest to largest. forexample, 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

Inputconsists of multiple problem instances. each instance starts with a linecontaining two positive integers n and M. the first value indicated the numberof objects to sort, where 2 <= n <= 26. the objects to be sorted will bethe first n characters
Of the uppercase alphabet. the second value m indicatesthe Number of relations of the form a <B which will be given in thisproblem instance. next will be m lines, each containing one such relationconsisting of three characters: an uppercase letter, the character "<"
And a second uppercase letter. No letter will be outside therange of the first n letters of the alphabet. Values of N = m = 0 indicate endof input.

Output

For eachproblem instance, output consists of one line. This line shocould be one of thefollowing 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 sortedsequence is determined or an inconsistency is found, whichever comes first, andyyy... Y is the sorted, ascending sequence.

Sampleinput

4 6

A <B

A <c

B <c

C <D

B <D

A <B

3 2

A <B

B <

26 1

A <z

0 0

Sampleoutput

Sorted sequence determined after 4 Relations: ABCD.

Inconsistency found after 2 relations.

Sorted sequence cannot be determined.

Source

Eastcentral North America 2001

Algorithm: topological sorting in graph theory.

Question: Match strings in ascending order.

About input:

The question contains a series of test examples.

First, give you two numbers n (n> = 2 & n <= 26) and M in the first line.

N indicates that a total of n different characters must be matched;

M gives you m characters in the form of "A <B;

Then, of course, the next m row enters these relationships in the form of "A <B.

If 0 is entered, the program is terminated.

Output:

There are three types of question output:

First, there is a conflict between characters.(PS: generates loops)

For the two characters 'A' and 'B', if one line of input (or according to the previous input) is "A <B ", in the Q (1 <q <= m) line, enter (or based on the input) "A> B ". then there is a conflict.

Output: Inconsistency found after qrelations.

For example, example 2 in the question is the case.

3 2

A <B

B <

The second line is in conflict with the first line. (Ring appears)

Note:For example 2, the input cannot match, but the output does not, therefore, when writing a program, you should pay attention to the output sequence of the two cases first to determine whether they are in conflict,

If there is a conflict, output the conflict directly and enter the next round of examples.

Case 2: The character cannot be matched successfully.

Output: sorted sequence cannot be determined.

Example 3: 26 1

A <z

It must match 26 characters, but only two characters are entered.

(PS: Enter N, it must match the first n uppercase letters ).

Case 3: character matching successful:

Output: sorted sequence determined after N relations:

Output characters in ascending order.

For example, output in Example 1:

Sorted sequencedetermined after 4 Relations: ABCD.

Topological sorting:

1. Select a vertex in the directed graph without a forward (that is, the inbound degree is 0) and output it.

2. Delete the output vertex and all edges ending with this vertex in the directed graph.

3. If no vertex with an input degree of 0 is found in the figure, the vertex ends. Otherwise, the vertex is converted to ①.

PS: partial order indicates that only some elements in the set can be compared in size (or in sequence );

Full Order means that all elements in a set can be compared to the size (or order ).

Topological sorting is to obtain the full-order relationship from the partial-order relationship.

Algorithm idea comes from: courseware 16 directed acyclic graph and its application.

Reference cool man code: http://blog.163.com/wojiaojason@126/blog/static/1240982842010214105332122/

Code 1:

// Accepted228k0msc ++ 1791b # include <stdio. h> # include <string. h> int map [27] [27]; int in_degree [27], CNT [27], sort [27]; int n, m; char STR [4]; int TOPO () {int I, j; int num, flag, now = 0, s; flag = now = 0; memset (sort, 0, sizeof (SORT )); for (I = 1; I <= N; I ++) CNT [I] = in_degree [I]; for (I = 1; I <= N; I ++) {num = 0; For (j = 1; j <= N; j ++) {If (CNT [J] = 0) {num ++; S = J;} // use CNT to record, so that the value of the sum array is not modified;} If (num = 0) return 0; // if there is no point with an inbound degree of 0, it indicates that there is a ring and the IF (n Um> 1) Flag = 1; // only one vertex with an inbound value of 0 can be sorted. Easy to error: direct return-1; sort [now ++] = s; // enter the CNT [s] =-1; // mark the queued for (j = 1; j <= N; j ++) if (Map [s] [J]) CNT [J] --; // Delete edge} If (FLAG) Return-1; // return 1;} int main () {int I, j; int ans, sign; int U, V; while (scanf ("% d", & N, & M )! = EOF) {If (n = 0 | M = 0) break; Sign = 0; // mark the result memset (MAP, 0, sizeof (MAP )); memset (in_degree, 0, sizeof (in_degree); for (I = 1; I <= m; I ++) {scanf ("% s", STR ); if (sign) continue; u = STR [0]-'A' + 1; V = STR [2]-'A' + 1; map [u] [v] = 1; // digital character record in_degree [v] ++; // record input degree ans = TOPO (); If (ANS = 0) // conflict {printf ("inconsistency found after % d relations. \ n ", I); Sign = 1;} If (ANS = 1) // The result is sorted successfully and {printf (" sorted sequence determined after % D relations: ", I); For (j = 0; j <n; j ++) printf (" % C ", sort [J] + 'a'-1); printf (". \ n "); Sign = 1 ;}} if (! Sign) // printf ("sorted sequence cannot be determined. \ n");} return 0 ;}

Error-prone analysis: (by kuangbin)

Why is the number of vertices whose input degree is 0? Num> 1 does not directly return-1, but uses flag.

Counterexample: For a cycle chart, a <B, B <C, and C <A are in conflict.

For example, if this graph is used, it should return 0.

It is incorrect to directly return-1. Although there is no conflict, it may be a conflict later.

See the following input. If there is no flag

5 5

A <B

B <c

C <

Inconsistency found after 4 relations.

But the correct result should be:

5 5

A <B

B <c

C <

Inconsistency found after 3 relations.

If you cannot understand graph theory, you can view the code of another person on the Internet:

/Source:/http://blog.sina.com.cn/s/blog_5ced353d0100b4xs.html

// AC 184kb 0 ms

Ø train of thought: Compare each character according to the input. For example, if there are n characters, we need to compare num = N (n-1) /Two Different "A <B" forms.

If the match is successful, there will certainly be a comparison of Characters in the form of num in different "A <B.

Ø how to compare: Because n <= 26, a comparison array cmpmap [26] [26] is set up to store the size relationship between letters, cmpmap [I] [J] = 0 indicates that the size of the I and j letters is not fixed;

1 indicates that the number I is less than the number J;

-1 indicates that the I-th letter is greater than the J-th letter.

All values before the M-Character relationship are assigned 0, indicating that the relationship between the characters is unknown.

 

About the relationship between the input m characters (A <B form:

Use an array s [4] for storage.

Ø if the current input is s [3], if s [0] = s [2], there is a conflict,

Ø if s [0]! = S [2] and cmpmap [s [0]-'a'] [s [2]-'a'] =-1, there are also contradictions

Ø if s [0]! = S [2] and cmpmap [s [0]-'a'] [s [2]-'a'] = 1, s [0] And s [2] are ordered, so no operation is performed.

Ø if s [0]! = S [2] and cmpmap [s [0]-'a'] [s [2]-'a'] = 0, scan all letters less than s [0], s [0], subscript is I, all letters greater than s [2], s [2], subscript is J, if cmpmap [I] [J] = 1, the order is sorted and no operation is performed. If cmpmap [I] [J] = 0, set cmpmap [I] [J] = 1, cmpmap [J] [I] =-1, num ++.

Ø num indicates the number of elements whose value is 1 in cmpmap. We can see that when num = N * (n-1)/2, all the letters are sorted in order. (As mentioned above)

Ø key point: there are 3 Characters in S, so it should be char s [4] and be written as char s [3]. The segmentation fault error occurs. Note: '\ 0 '.

 

# Include <stdio. h> int cmpmap [26] [26], biggernum [26]; int main () {// freopen ("in.txt", "r", stdin); INTN, m, i, j, num, Q, inconsistency, succeed; chars [4]; while (scanf ("% d", & N, & M )) {If (n = 0 & M = 0) break; num = 0; inconsistency = 0; succeed = 0; for (I = 0; I <N; I ++) for (j = 0; j <n; j ++) cmpmap [I] [J] = 0; For (q = 1; q <= m; Q ++) {scanf ("% s", S ); if (s [0] = s [2] | cmpmap [s [0]-'a'] [s [2]-'a'] =-1) // conflicts between S [0] And s [2] {inconsistency = 1; break ;} if (cmpmap [s [0]-'a'] [s [2]-'a'] = 1) // s [0] And s [2] ordered continue; for (I = 0; I <n; I ++) // s [0] And s [2] unordered if (cmpmap [s [0]-'a'] [I] =-1 | (s [0]- 'A ') = I) // scan all letters less than s [0] And s [0] {for (j = 0; j <n; j ++) if (cmpmap [s [2]-'a'] [J] = 1 | (s [2]-'A') = J) & cmpmap [I] [J] = 0) // scan all letters larger than s [2] and S [2] {// cmpmap [I] [J] = 0 indicates that the I and j letters are unordered. cmpmap [I] [J] = 1; cmpmap [J] [I] =-1; num ++;} If (num = N * (n-1)/2) // num = N * (n-1) /2 indicates that all letters have been sorted {succeed = 1; break ;}} if (inconsistency) printf ("inconsistencyfound after % d relations. \ n ", q); elseif (succeed) {printf (" sortedsequence determined after % d relations: ", q ); // output the sorted letter sequence for (I = 0; I <n; I ++) biggernum [I] = 0; for (I = 0; I <N; I ++) for (j = 0; j <n; j ++) if (cmpmap [I] [J] = 1) biggernum [I] ++; // find out how many letters are larger than I for (I = n-1; I> = 0; I --) {for (j = 0; j <n; j ++) if (biggernum [J] = I) {printf ("% C", 'A' + J); break ;}} printf (". \ n ");} else printf (" sortedsequence cannot be determined. \ n "); If (q <m) for (Q ++; q <= m; q ++) scanf (" % s ", S) ;} return0 ;}

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.