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 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

--------------------------------------
所有方案,需要回溯,用Kahn比较好
L← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
foreach node m with an edge e from nto m do
remove edge e from thegraph
ifm has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least onecycle)
else
return L (a topologically sortedorder)

就是找入度为0的点(最好用个stack,循环的话复杂的太高),加入topo头部

感觉比dfs好,复杂度都是O(V+E)

本题回溯所有方案,复杂度乘上一个V;V很小,不用stack也可以;用个id比较方便吧

字符读入太坑人.........

//
// main.cpp
// poj1270
//
// Created by Candy on 9/11/16.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=,M=;
char s[];
int a[N],num=,n=,id[N];
int ch[N][N],topo[N],ind[N]; void print(){
for(int i=;i<=n;i++) printf("%c",(char)topo[i]+'a'-);
printf("\n");
}
void dfs(int d){ //printf("dfs %d\n",d);
if(d==n+){print();return;}
for(int i=;i<=n;i++)
if(ind[i]==){
ind[i]--; topo[d]=a[i];
for(int j=;j<=ch[i][];j++) ind[ch[i][j]]--;
dfs(d+);
for(int j=;j<=ch[i][];j++) ind[ch[i][j]]++;
ind[i]++;
}
}
int main(int argc, const char * argv[]) {
while(fgets(s,,stdin)){ //printf("p %s\n",s);
n=;
memset(topo,,sizeof(topo));
memset(ch,,sizeof(ch));
memset(ind,,sizeof(ind));
int len=strlen(s); //printf("len %d\n",len);
for(int i=;i<len;i++)
if(s[i]>='a'&&s[i]<='z') a[++n]=s[i]-'a'+;
sort(a+,a++n);
for(int i=;i<=n;i++) id[a[i]]=i; fgets(s,,stdin);
len=strlen(s);
int last=;
for(int i=;i<=len;i++)
if(s[i]>='a'&&s[i]<='z'){
int t=s[i]-'a'+;
t=id[t];
if(last==) last=t;
else{ch[last][++ch[last][]]=t;ind[t]++;last=;}
}
dfs();
printf("\n");
}
return ;
}

POJ1270 Following Orders[拓扑排序所有方案 Kahn]的更多相关文章

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

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

  2. POJ 1270 Following Orders 拓扑排序

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

  3. POJ1270 Following Orders (拓扑排序)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4254   Accepted: 1709 ...

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

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

  5. AOV网络和Kahn算法拓扑排序

    1.AOV与DAG 活动网络可以用来描述生产计划.施工过程.生产流程.程序流程等工程中各子工程的安排问题.   一般一个工程可以分成若干个子工程,这些子工程称为活动(Activity).完成了这些活动 ...

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

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

  7. 拓扑排序+DFS(POJ1270)

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

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

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

  9. 2017-2018 ACM-ICPC NEERC B题Berland Army 拓扑排序+非常伤脑筋的要求

    题目链接:http://codeforces.com/contest/883/problem/B There are n military men in the Berland army. Some ...

随机推荐

  1. HoverTree菜单0.1.3新增效果

    HoverTree菜单0.1.3增加弹出菜单的动态效果,可以是动态下拉,也可以是动态淡入. 效果请看:http://keleyi.com/jq/hovertree/demo/demo.0.1.3.ht ...

  2. JQuery插件Validation的使用-遁地龙卷风

    第二版 (-1)写在前面 本文不是要详细说明Validation插件的使用,而是将满足开发需求的代码已最应该使用的方式写出来,并附有详细的注释 想要了解更多,去jquery的官网,有最新,最全面的,后 ...

  3. SAP学习日志--RFC REMOTE FUNCTION CALL

    RFC  Remote function Call 远程功能调用, 是SAP系统之间以及非SAP系统之间程序通信的基本接口技术. 例如BAPI , ALE都是基于RFC实现的 SAP系统提供了三种外部 ...

  4. ORA-00054:资源正忙,但指定以nowait方式

    PL/SQL执行SQL脚本文件,报错如下: 百度寻找答案,认为是被锁了. select session_id from v$locked_object; 结果没有任何数据.   后来把PL/SQL关闭 ...

  5. [outlook]打开以后就自动进入安全模式的解决方法。Outlook start in safe mode.

    给客户写了一个Outlook的add-in, 用现在时髦的话应该叫outlook的app. 这个add-in的作用就是把outlook中的email,直接上传到SharePoint中.想要代码的联系我 ...

  6. JavaScript学习07 内置对象

    JavaScript内置对象 图像对象 导航对象 窗口对象 屏幕对象 事件对象 历史对象 文件对象(重要) 锚点对象 链接对象 框架对象 表单对象(重要) 位置对象 JS Window 窗口对象:ht ...

  7. 在eclipse中把之前的Tomcat 6删掉,不能再建

    在eclipse中把之前的Tomcat 6删掉,重新配置一个,不料没有下一步. 解决的方法了,如下: 1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.ec ...

  8. 网络初见&网络监测

    这里需要下载一个第三方 Reachability-master 大家可以百度一下 下载之后把 Reachability拖进来 具体代码如下 #import "ViewController.h ...

  9. MyEclipse、Eclipse优化设置

    第一步: 取消自动validation validation有一堆,什么xml.jsp.jsf.js等等,我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: windows ...

  10. MPlayerX——MAC OS 最好用的播放器

    MPlayerX真是一个不错的软件,它真的可以称得上在MAC OS里最好用的播放器,它功能强大,可以播放你所知道的任何格式的视频和音频文件.他的选项非常丰富,可以自定义设置的东西很多,但又不失简洁的风 ...