POJ1270 Following Orders(拓扑排序)

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

Duke Internet Programming Contest 1993,uva 124

题意:给定一串字符(互异),再给出一个字符序列,表示一种前后关系,如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);
    }
}
时间: 2024-10-10 17:49:01

POJ1270 Following Orders(拓扑排序)的相关文章

POJ1270 Following Orders[拓扑排序所有方案 Kahn]

Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 Description Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which

poj 1270 Following Orders(拓扑排序+dfs)

大致题意:每个样例包含两行,第一行输入n个字符,可能是无序的.第二行输入成对的a b,代表a要在b前面.输出所有的符合这样的序列. 思路:很明显的拓扑排序.要输出所有的序列,那么就从入度为0的点进行dfs,每次选择一个入度为0的点,加入输出序列并把与它相邻的点的入度减一.dfs结束后要把状态再改回来. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include

UVA 124 &amp; POJ 1270 Following Orders(拓扑排序)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60 http://poj.org/problem?id=1270 Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3806   Accepted: 1507 Description Or

POJ 1270 Following Orders 拓扑排序全输出

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 a

POJ1270 Following Orders (拓扑排序)

Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4254   Accepted: 1709 Description Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which

poj1270拓扑排序

题意:给定一些大小关系,把关系从大到小排序,如果有多种相同关系就按字典序排序.例如 x < y && x < z 则满足关系的有xyz 和 xzy . 思路:我们可以把每组大小关系建一条有向边,这样就形成了一个有向图,再对有向图进行拓扑,得到的结果即满足.但是拓扑出来的是一组可行解,而题意是要输出所有可行解,刚好可以想到递归求拓扑时可以回溯:那样就可以把所有的可行解输出.不过要把所有的变量排序. 代码: #pragma warning (disable: 4786) #incl

poj1270Following Orders(拓扑排序+dfs回溯)

题目链接: 啊哈哈,点我点我 题意是: 第一列给出所有的字母数,第二列给出一些先后顺序.然后按字典序最小的方式输出所有的可能性... 思路: 总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了..那么这个问题就得到了解决.. 题目: Following Orders Time Limit:

CF 412 D Giving Awards(拓扑排序)

The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO

POJ--1128--Frame Stacking【拓扑排序】

链接:http://poj.org/problem?id=1128 题意:有几张图片,给你叠加到一起之后的图,问叠加的可能性,如有多种可能则按字典序由小到大输出. 思路:根据给出的图形建一个图,被覆盖的图片向覆盖它的图片建边,然后拓扑排序. 拓扑排序按照字母顺序从小到大找入度为0的点,用dfs形式的拓扑排序,就按照字典序输出了. POJ1270的做法也类似: 代码 #include<cstring> #include<string> #include<fstream>

求拓扑排序的数量,例题 topcoder srm 654 div2 500

周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are numbered from 0 to N-1. Some pairs of rooms are connected by bidirectional passages. The passages have the topology of a tree. That is, there are exactly N-