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-;
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 = ; i < ; i++)
{
if(vis[i]) continue;
if(in[i] == )
{
ans[id] = 'a' + i;
vis[i] = ;
for(int j = ; j < ; j++)
if(map1[i][j]) in[j]--;
dfs(id+);
vis[i] = ;
for(int j = ; j < ; j++)
if(map1[i][j]) in[j]++;
}
}
}
char x, y;
int main()
{
int flag = ;
while(gets(a) != NULL)
{
if(flag)
puts("");
flag = ;
total = ;
stringstream ss(a);
memset(in, INF, sizeof in);
memset(vis, , sizeof vis);
while(ss >> x)
{
in[x - 'a'] = ;
total++;
}
gets(a);
stringstream sss(a);//读取一行。
memset(map1, , sizeof map1);
while(sss >> x >> y)//扫描该行的字符。
{
map1[x - 'a'][y- 'a'] = ;
in[y - 'a']++; }
dfs();
}
return ;
}

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

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

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 ...

  2. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  3. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  4. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

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

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  6. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  7. POJ 1270 Following Orders(拓扑排序)

    题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...

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

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  9. 【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348

    目录 Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348 PROBLEM 题目描述 输入 输出 样例输入 样例输出 提示 MEA ...

随机推荐

  1. Axure 原型设计工具画业务流程图

    加入人人都是产品经理[起点学院]产品经理实战训练营,BAT产品总监手把手带你学产品点此查看详情! 软件行业从业6年,流程图看过太多,大部分流程图是在考验阅读者的理解能力,近期在设计公司新版APP,对流 ...

  2. Linux下获取线程TID的方法——gettid()

    (转载)http://blog.csdn.net/delphiwcdj/article/details/8476547 如何获取进程的PID(process ID)? 可以使用: #include & ...

  3. iOS AFNetworking 详解

    1. 很不错的介绍 http://m.blog.csdn.net/blog/jackljf/38736625

  4. 【实用技术】DreamWeaver常用快捷键

    文件菜单 新建文档 Ctrl+N 打开一个HTML文件 Ctrl+O 或者将文件从[文件管理器]或[站点]窗口拖动到[文档]窗口中 在框架中打开 Ctrl+Shift+O 关闭 Ctrl+W 保存 C ...

  5. Ubuntu 启动器/快捷方式/ 制作 (Eclipse为例)

    首先,在路径/usr/share/applications/,中创建eclipse.desktop(如果没有的话) sudo touch /usr/share/applications/eclipse ...

  6. [转载]Linux编程 sockaddr_in 和sockaddr和in_addr详解

    sockaddr sockaddr 是通用的socket地址,具体到Internet socket,用下面的结构,二者可以进行类型转换 sa_family是地址家族,一般都是"AF_xxx& ...

  7. fcitx-rime添加五笔/五笔拼音

    简介 Rime,全名:中州韵输入法引擎,它不仅仅是一个输入法,也是一个输入法算法框架.Rime 支持拼音.双拼.注音.五笔和仓颉等音码.形码输入法. 安装 Debian sid 用户可以使用下面命令安 ...

  8. flashback database操作步骤

    默认情况数据库的flashback database是关闭的. 启用Flashback Database 步骤:1.配置Flash Recovery Area 检查是否启动了flash recover ...

  9. 几个Linux常见命令

    ls 作用:查看目录下的文件 格式:直接ls查看当前所在目录,或者  ls 参数 /目录名 参数: -l 查看详细信息 -a 显示隐藏文件 .   表示当前目录 ..  上级目录 -G 用不同颜色标记 ...

  10. struts2 实现过程和xml配置

    实现过程: 当Web容器收到 请求(HttpServletRequest)它将请求传递给一个标准的的过滤链包括 (ActionContextCleanUp)过滤器,然后经过Other filters( ...