Following Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5473   Accepted: 2239

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

题目大意:

给你一个有向无环图,请你按字典序输出它所有的toposort结果。

回溯法。

回溯时如果使用了全局变量,要在递归出口处立即还原该全局变量,如ac代码中的vis数组和path数组。

此题输入输出好坑啊。用gets才行,I also not kow why。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
typedef long long ll;
const int maxn=; char str1[],str2[];
int exis[];//26个lowercase字母
int tot;//一共存在多少字母
int cnt;
int to[];
int next[];
int head[];
int in[];//入度
int vis[];
char path[]; void dfs(int x,int m)
{
if(m==tot-)
printf("%s\n",path);
for(int i=head[x];i!=-;i=next[i])
{
int l=to[i];
if(!vis[l])
in[l]--;
}
for(int i=;i<;i++)
{
if(!vis[i]&&exis[i]&&in[i]==)
{
vis[i]=;
path[m+]='a'+i;
dfs(i,m+);
path[m+]='\0';
vis[i]=;
}
}
for(int i=head[x];i!=-;i=next[i])
{
int l=to[i];
if(!vis[l])
in[l]++;
}
} int main()
{
int k=;
while(gets(str1))
{
gets(str2);
if(k>)
printf("\n");
k++; memset(exis,,sizeof(exis));
tot=;
for(int i=;str1[i]!='\0';i++)
{
if(str1[i]>='a'&&str1[i]<='z')
{
exis[str1[i]-'a']=;
tot++;
}
}
cnt=;
memset(head,-,sizeof(head));
memset(in,,sizeof(in));
for(int i=;str2[i]!='\0';i+=)
{
int u=str2[i]-'a',v=str2[i+]-'a';
to[cnt]=v;next[cnt]=head[u];head[u]=cnt++;
in[v]++;
} memset(vis,,sizeof(vis));
for(int i=;i<;i++)
path[i]='\0';
for(int i=;i<;i++)
{
if(exis[i]&&in[i]==)
{
vis[i]=;
path[]='a'+i;
dfs(i,);
path[]='\0';
vis[i]=;
}
}
}
return ;
}

poj 1270 Following Orders (拓扑排序+回溯)的更多相关文章

  1. POJ 1270 Following Orders 拓扑排序

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

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

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

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

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

  4. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  5. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

  6. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  7. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  8. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  9. POJ 2585.Window Pains 拓扑排序

    Window Pains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1888   Accepted: 944 Descr ...

  10. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

随机推荐

  1. 五年老Android,我决定转后端开发了!

    今天给大家分享一些移动端(Android)开发学习后端开发(Java Web)的一些事儿,不知道从什么时候开始身边的同事都开始陆陆续续的在朋友圈发一些后端的文章如:Nginx.Docker.k8s类的 ...

  2. ArcGIS 重新创建几何服务(GeometryService)

    #参考官方网址:http://enterprise.arcgis.com/zh-cn/server/10.4/administer/windows/re-creating-the-geometry-s ...

  3. Java w3c离线手册

    提供给大家使用,懒得找: 前端后端一般都用的到  查看文档 1. JDK_API_1_6_zh_CN.CHM 2.     W3School离线手册(2018.04.01).chm 3.    jqu ...

  4. docker实例之mysql的使用

    docker实例之mysql的使用 常用步骤 命令 1:搜索镜像 docker search xxx 2:拉取镜像 docker pull xxx:yy 3:查看镜像 docker image ins ...

  5. JavaScript笔记八

    1.原型(prototype) - 创建一个函数以后,解析器都会默认在函数中添加一个数prototype prototype属性指向的是一个对象,这个对象我们称为原型对象. - 当函数作为构造函数使用 ...

  6. Python和BeautifulSoup进行网页爬取

    在大数据.人工智能时代,我们通常需要从网站中收集我们所需的数据,网络信息的爬取技术已经成为多个行业所需的技能之一.而Python则是目前数据科学项目中最常用的编程语言之一.使用Python与Beaut ...

  7. 这事没完,继续聊spring cloud stream和kafka的这些小事

    上一篇文章讲了如何用spring cloud stream集成kafka,并且跑起来一个demo,如果这一次宣传spring cloud stream的文章,其实到这里就可以啦.但实际上,工程永远不是 ...

  8. 部署高可用 schduler

    目录 创建 kube-scheduler 证书和私钥 创建和分发 kubeconfig 文件 创建 kube-scheduler 配置文件 创建kube-scheduler启动文件 启动kube-sc ...

  9. vsftp安装和配置

    2013-10-10 10:49:48|  分类: Linux|举报|字号 订阅     源码下载地址:ftp://vsftpd.beasts.org/users/cevans/untar/   1. ...

  10. VLAN实验3(Hybrid接口的应用)

    本实验基于<HCNA网络技术实验指南> 本实验使用eNSP软件 原理概述: Hybrid接口既可以连接普通终端的接入链路又可以连接交换机间的干道链路,它允 许多个VLAN的帧通过,并可以在 ...