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. 工具:BT Sync 同步文件

    随着互联网的发展,文件共享变得越来越便捷,但是文件的共享过程是不是安全,这一直是人们关心的问题,今天向大家介绍一个共享工具,可以实现便捷的安全共享.      这个工具分为两个部分,一个是服务器部分, ...

  2. LeetCode198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. ios 导航栏和旋屏

    1,状态栏(UIStatusBar) http://my.oschina.net/shede333/blog/304560 2,visibleViewController和topViewControl ...

  4. Android应用开发学习之启动另外一个Activity

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一个Activity可以启动另外一个Activity,以实现比较复杂的功能,我们来看一个例子,其运行效果如下图所示: ...

  5. 开机后将sim/uim卡上的联系人写入数据库

    tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51 ...

  6. 批量创建prefab

    using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; using UnityEdito ...

  7. SEO 外链 内链 的定义

    外链 外链就是指从别的网站导入到自己网站的链接.导入链接对于网站优化来说是非常重要的一个过程.导入链接的质量(即导入链接所在页面的权重)直接决定了我们的网站在搜索引擎中的权重. 外链是互联网的血液,是 ...

  8. CPU性能测试

    用计算圆周率的办法来测试cpu性能 4*a(1) 是 bc 主動提供的一個計算 pi 的函數,至於 scale 就是要 bc 計算幾個小數點下位數的意思.當 scale 的數值越大, 代表 pi 要被 ...

  9. String和StringBuilder 的使用区别

    String 类有不可变性,每次执行操作时都会创建一个新的String对像,需要对该对象分配新的空间. StringBuilder 解决了对字符串重复修改过程中创建大量对象的问题.初始化一个Strin ...

  10. 我的第一个QML Button的实现

    编写第一个QML,在成功跑完HelloWorld后,决定自己实现Button按钮类. Button是在Quick2版本以上的QtQuick Controls出现的. 在Qt5.5.1版本中,选择插入Q ...