[ACM] POJ 1094 sorting It all out (topological sort)

Source: Internet
Author: User

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26801 Accepted: 9248

Description

An ascending sorted sequence of distinct values are one in which some form of a less-than operator are used to order the Ele ments from smallest to largest. For example, the sorted sequence A, B, C, D implies, a < B, b < C and C < D. In this problem, we'll give yo U a set of relations of the form a < B and ask you to determine whether a sorted order have been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing-positive integers n and m. The first value indicated the number of objects To sort, where 2 <= n <= 26. The objects to be sorted'll be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which'll be given in this problem instance. Next'll be M lines, each containing one such relation consisting of three Characters:an uppercase letter, the character "<" and a second uppercase letter. No letter would 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, the output consists of one line. This line should is 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 are determined or an inconsistency is F Ound, 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.

Source

East Central North America 2001


Problem Solving Ideas:

Application of topological sequencing. The http://www.cnblogs.com/pushing-my-way/archive/2012/08/23/2652033.html did.

There are a lot of problems to be aware of and a little "pit". The following is a transfer from the above blog, () inside the content is my own Add.

Test instructions: Give you some partial-order relationships between capital letters, and then let you infer whether you can uniquely determine the relationship between them, or if the relationship is contradictory, or in the end it is impossible to determine the relationship between them.

Analysis:

Sort by topology:

1. Topology sequencing can be implemented with stacks, each of which is a node with an entry level of 0 (which can also be used in a queue, or not using queues and stacks, looping n times, and finding a point with a degree of 0).

1. The results of topological sequencing are generally divided into three cases: 1, can infer (topological ordering has a unique result) 2, there is a conflict (there is no penetration of 0 nodes) 3, insufficient conditions, can not be inferred.

2. This problem requires not only the inference of these three cases, but also the inference that in the first two cases when dealing with the first relationship, three cases are prioritized for this problem. The first two cases are equal who first appear first to output the corresponding results, for the third case is not in the first two cases under the premise of the output of the corresponding results.

Online error tips for this problem (note):

1. Order of the subject:

A. First, there is no ring, the direct output of the ring can not be determined;

B. Assuming there is no ring, then see if there are a variety of circumstances, assuming that there are a number of cases to read the next line, assuming that all the lines read or there are many situations, is not OK

C. Assuming that there is no ring at the end, there are no multiple cases (that is, there is only one point at which the zero-per-entry is taken), then the answer is.

2. There is the answer to the first answer, no matter what the future will be contradictory or something;

3. Assume that you can answer the question without reading the complete input, and be sure to read the rest of the line.

Code:

#include <iostream> #include <algorithm> #include <string.h> #include <stack> #include < Queue>using namespace Std;int indegree[30];//save in degrees int graph[30][30];//whether there is an edge char output[30];//output to determine the sequence bool ok;//    Can be determined that the bool dilemma;//has a ring, the contradiction bool no;//cannot be determined char c1,c,c2;//input int topo (int n) {int in[30];         for (int i=0;i<n;i++) in[i]=indegree[i];//uses an alternate array to sort the topology stack<int>s;//the 0-degree point into the stack for (int i=0;i<n;i++)    if (!in[i]) s.push (i); BOOL flag=0;//stack with a 0 element greater than one, an indeterminate topological sort int cnt=0;//the number of elements in the 0, is also the output sequence number while (!s.empty ()) {if (S.size ())        >1) flag=1;//indeterminate int first=s.top ();        S.pop ();            output[cnt++]=first+ ' A ';//Enter the output sequence inside for (int i=0;i<n;i++) if (Graph[first][i])//element connected to the element with an entry level of 0 {            in[i]--; if (in[i]==0) s.push (i),///Into the stack}} if (Cnt!=n)//If there is no ring, the number of elements in the sequence is certainly equal to the number of elements entered, even if an element is not entered, its degree is also initialized For 0 return 2;//have ring else if (flag==1)//indeterminateThe topological sort return-1; return 1;}    int main () {int n,m; while (cin>>n>>m&& (n| |        m)) {ok=0;dilemma=0;no=0;        memset (indegree,0,sizeof (Indegree));        memset (graph,0,sizeof (graph));            for (int i=1;i<=m;i++) {cin>>c1>>c>>c2;//When there is a contradiction or a sequence can be determined by some conditions, the remaining input conditions do not need to be processed again. if (!ok&&!dilemma)//no ring, no definite topological sort, here the topological sort must enter the letters have.                For example input ABCD then only AB is not deterministic {int t1=c1-' A ';                int t2=c2-' A '; if (GRAPH[T2][T1])//bidirectional side, with ring, appears contradictory {cout<< "inconsistency found after" &LT;&LT;I&LT;&L t; "Relations."                    <<endl;                Dilemma=1;//appeared contradictory continue;                    } if (!graph[t1][t2]) {graph[t1][t2]=1;                indegree[t2]++;//+ +} int ans=topo (n);//OK return 1, with loop return 2 if (ans==2)        {            cout<< "inconsistency found after" <<i<< "relations."                    <<endl;                    Dilemma=1;                Continue } if (ans==1) {cout<< "Sorted sequence determined after" &LT;&LT;I&L                    t;< "Relations:";                    for (int k=0;k<n;k++) cout<<output[k]; cout<< "."                    <<endl;                ok=1; }}} if (!ok&&!dilemma) cout<< "Sorted sequence cannot be determined."    <<endl; } return 0;}



[ACM] POJ 1094 sorting It all out (topological sort)

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.