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

题目链接:

啊哈哈,点我点我

题意是:

第一列给出所有的字母数,第二列给出一些先后顺序。然后按字典序最小的方式输出所有的可能性。。。

思路:

总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了。。那么这个问题就得到了解决。。

题目:

Following Orders

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3800   Accepted: 1502

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

代码为:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;

const int maxn=26+2;
char str[maxn],ans[maxn],apa[maxn];
int in[maxn],gra[maxn][maxn],res;
map<char,int>mp;

void topo(int depth)
{
    if(depth==res)
    {
        printf("%s\n",ans);
        return;
    }
    for(int i=0;i<res;i++)
    {
        if(in[i]==0)
        {
            --in[i];
            ans[depth]=apa[i];
            for(int j=0;j<res;j++)
            {
                if(gra[i][j])
                   --in[j];
            }
            topo(depth+1);
            ++in[i];
            for(int j=0;j<res;j++)
            {
                if(gra[i][j])
                   ++in[j];
            }
        }
    }
}

int main()
{
    int flag,k,len;
    char temp1,temp2;
    while(gets(str))
    {
        k=0;
        memset(ans,0,sizeof(ans));
        memset(in,0,sizeof(in));
        memset(gra,0,sizeof(gra));
        len=strlen(str);
        for(int i=0;i<len;i++)
           if(str[i]>='a'&&str[i]<='z')
                apa[k++]=str[i];
        sort(apa,apa+k);
        for(int i=0;i<k;i++)
            mp[apa[i]]=i;
        res=k;
        gets(str);
        len=strlen(str);
        for(int i=0;i<len;i++)
            if(str[i]>='a'&&str[i]<='z')
        {
            if(flag)
            {
                temp1=str[i];
                flag=0;
            }
            else
            {
                temp2=str[i];
                gra[mp[temp1]][mp[temp2]]=1;
                in[mp[temp2]]++;
                flag=1;
            }
        }
       topo(0);
       printf("\n");
    }
    return 0;
}

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

时间: 2024-08-19 09:28:27

poj1270Following Orders(拓扑排序+dfs回溯)的相关文章

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

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

ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083))

经典的拓扑排序问题,难点在于字典序输出和建立拓扑图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacking 题意:每个图片由同一字母组成的边框表示,每个图片的字母都不同: 在一个最多30*30的区域放置这些图片,问底层向顶层叠加的图片次序,多选时按字典序输出 注:每个图片的四边都会有字符显示,其中顶点显示两边. 题解:题意的理解是难点,题目对图片的范围确定说得有点含糊不清,博主一开始就被出现的五张图片的样例迷惑,理解重心放错了.题目最需要理解的是

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

HDU 5438 拓扑排序+DFS

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3234    Accepted Submission(s): 997 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, a

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

拓扑排序/DFS HDOJ 4324 Triangle LOVE

题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记,否则超时.看是否存在两点路程只差等于2,如果存在,则说明有上述的三角环.其他做法. 收获:DFS搜索一定要用vis数组啊,否则很容易超时的 代码(拓扑排序): /************************************************ * Author :Running_T

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[拓扑排序所有方案 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