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 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
收获:1.了解了stringstream.    2.用dfs输出拓扑排序的所有情况。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <sstream>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 500
#define maxm 28
char a[maxn];
char ans[maxn];
int in[maxn];
int vis[maxn];
int map1[maxn][maxn];
int total;
void dfs(int id)
{
    if(id == total)
    {
        ans[id] = ‘\0‘;
        puts(ans);
        return;
    }
    for(int i = 0; i < 26; i++)
    {
        if(vis[i]) continue;
        if(in[i] == 0)
        {
            ans[id] = ‘a‘ + i;
            vis[i] = 1;
            for(int j = 0; j < 26; j++)
                if(map1[i][j]) in[j]--;
            dfs(id+1);
            vis[i] = 0;
            for(int j = 0; j < 26; j++)
                if(map1[i][j]) in[j]++;
        }
    }
}
char x, y;
int main()
{
    int flag = 0;
    while(gets(a) != NULL)
    {
        if(flag)
            puts("");
        flag = 1;
        total = 0;
        stringstream ss(a);
        memset(in, INF, sizeof in);
        memset(vis, 0, sizeof vis);
        while(ss >> x)
        {
        in[x - ‘a‘] = 0;
        total++;
        }
        gets(a);
        stringstream sss(a);//读取一行。
        memset(map1, 0, sizeof map1);
        while(sss >> x >> y)//扫描该行的字符。
        {
            map1[x - ‘a‘][y- ‘a‘] = 1;
            in[y - ‘a‘]++;

        }
        dfs(0);
    }
    return 0;
}
时间: 2024-08-01 06:01:57

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拓扑排序

题意:给定一些大小关系,把关系从大到小排序,如果有多种相同关系就按字典序排序.例如 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-