码迷,mamicode.com
首页 > 编程语言 > 详细

POJ1270 Following Orders(拓扑排序)

时间:2015-04-29 15:13:24      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:图论   tope   

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

Description

Order is an important concept in mathematics and in computer science. For example, Zorn‘s Lemma states: ``a partially ordered set in which every chain has an upper bound contains a maximal element.‘‘ 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 are to write a program that prints all orderings of the variables that are consistent with the constraints. 


For example, given the constraints x < y and x < z there are two orderings of the variables x, y, and z that are consistent with these constraints: x y z and x z y. 

Input

The input consists of a sequence of constraint specifications. A specification consists of two lines: a list of variables on one line followed by a 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 are single character, lower-case letters. There will be at least two variables, and no more than 20 variables in a specification. There will be at least one constraint, and no more than 50 constraints in a specification. There will be at least one, and no more than 300 orderings consistent with the contraints in a specification. 


Input is terminated by end-of-file. 

Output

For each constraint specification, all orderings consistent with the constraints should be printed. Orderings are printed in lexicographical (alphabetical) order, one per line. 


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

Sample Input

a b f g
a b b f
v w x y z
v y x v z v w v

Sample Output

abfg
abgf
agbf
gabf

wxzvy
wzxvy
xwzvy
xzwvy
zwxvy
zxwvy

Source

题意:给定一串字符(互异),再给出一个字符序列,表示一种前后关系,如a b e f c d,表示a<b,e<f,c<d。

将开始给出的字符进行排序,使之符合这个关系序列。并按字典序输出这些符合要求的字符序列。

#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]!='\0';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]!='\0')
        {
            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(拓扑排序)

标签:图论   tope   

原文地址:http://blog.csdn.net/u010372095/article/details/45365435

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!