POJ1270 following Orders (topological sorting)

Source: Internet
Author: User

Following Orders
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4059 Accepted: 1623

Description

Order is a important concept in mathematics and in computer. For example, Zorn ' Lemma states: "A partially ordered set in which every chain have an upper bound contains a maximal ele ment. " Order is also important in reasoning about the fix-point semantics of programs.


This problem involves neither Zorn ' s Lemma nor fix-point semantics, but does involve order.
Given a list of variable constraints of the form x < Y, you is to write a program this prints all orderings of the Var Iables that is consistent with the constraints.


For example, given the constraints x < Y and x < Z there is both orderings of the variables x, y, and z that is con Sistent with these constraints:x y Z and x z y.

Input

The input consists of a sequence of constraint specifications. A specification consists of both lines:a list of variables on one lineFollowed bya list of contraints on the next line. A constraint is given by a pair of variables, where x y indicates that x < Y.


All variables isSingle character,lower-case Letters. There'll is at least-variables, and no more than-variables in a specification. There'll is at least one constraint, and no more than the constraints in a specification. There'll is at least one, and no more than orderings consistent with the contraints in a specification.


Input is terminated by End-of-file.

Output

For each constraint specification,All orderings consistent and the constraints should be printed. Orderings is printed in lexicographical (alphabetical) order, one per line.


Output for different constraint specifications are separated by a blank line.

Sample Input

A b f GA b b fv W x y zv y x v z v W V

Sample Output

Abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy

Source

Duke Internet Programming Contest 1993,uva 124

Test instructions: Given a string of characters (inter-XOR), then a sequence of characters, indicating a back-and-forth relationship, such as a B E F C D, representing A<b,e<f,c<d.

Sorts the characters that begin to be given so that they conform to the relationship sequence. and output these matching character sequences according to the dictionary order.

#include <stdio.h> #include <vector> #include <string.h>using namespace std;const int N = 30;int In[n],    Exist[n],mapt[n][n],path[n],n;void topesort (int u,int k) {path[k]=u;        if (k==n) {for (int i=1;i<=n;i++) printf ("%c", path[i]+ ' a ');        printf ("\ n");    return;    } in[u]=-1;    for (int i=0;i<26;i++) if (Mapt[u][i]) in[i]-=mapt[u][i];    for (int i=0;i<26;i++) if (Exist[i]&&!in[i]) topesort (i,k+1);    in[u]=0; for (int i=0;i<26;i++) if (Mapt[u][i]) in[i]+=mapt[u][i];}    int main () {int flag=0;    Char str[1000];        while (gets (str)) {if (flag) printf ("\ n"); flag=1;        memset (In,0,sizeof (in));        memset (exist,0,sizeof (exist));        memset (mapt,0,sizeof (MAPT));        n=0;            for (int i=0;str[i]!= ' n '; i++) if (str[i]>= ' a ' &&str[i]<= ' z ') {int ch=str[i]-' a ';            if (exist[ch]==0) n++;        Exist[ch]=1;    }    Gets (str);        int i=0,a,b;            while (str[i]!= ' ") {while (str[i]== ') i++; a=str[i++]-' a '; while (str[i]== ') i++;            b=str[i++]-' a '; mapt[a][b]++;        in[b]++;    } for (int i=0;i<26;i++) if (Exist[i]&&!in[i]) topesort (i,1); }}


POJ1270 following Orders (topological sorting)

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.