POJ 1270 HOJ 1170 Following Orders

来源:互联网 发布:网络电视怎么看3d电影 编辑:程序博客网 时间:2024/06/05 08:57
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3057 Accepted: 1171

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 ga b b fv w x y zv y x v z v w v

Sample Output

abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy

 

 

 

 

 

蒽,综合了DFS和拓扑排序,可以这么想:

 

先建一个图,由于是这种严格偏序关系,想到拓扑排序。

 

我是这么理解拓扑的:

拓扑排序只有一种排法,如果所有的元素都规定好了关系。比如如果有四个元素,规定好a<b,b<c,c<d,这个排序就唯一了

但当他们没有完全规定好关系的时候。有多个拓扑子图。然后每步选择哪个子图,都随便。

 

对子拓扑图的选择,就是DFS的转移方法数

 

比如第一例,

GIVEN :A B F G

 

图1:

A<B,B<F

图2:

G

 

所以第一次可以选G,也可以选A

第一次如果选A,第二次可以选G、B

如此如此

#include <cstdio>#include <cstring>#include <algorithm>#define rep(i,a,b) for(int i=a;i<b;i++)#define INIT(a,b) memset(a,b,sizeof(a))typedef long long ll;using namespace std;char buf[1000],u,v,ch[30];bool g[30][30];int n,mp[260],uu,vv,in[30],pt[30];void dfs(int lv){if(lv==n){rep(i,0,n)putchar(ch[pt[i]]);putchar('\n');return ;}rep(i,0,n)if(!in[i]){pt[lv]=i;in[i]--;rep(j,0,n)if(g[i][j])in[j]--;dfs(lv+1);rep(j,0,n)if(g[i][j])in[j]++;in[i]++;}}int main(){int cas=0;while(gets(buf)){INIT(in,0);n=0;int pt=0,pt_1;while(u=buf[pt++]){if(u==' ')continue;ch[n++]=u;}sort(ch,ch+n);rep(i,0,n)mp[ch[i]]=i;INIT(g,0);gets(buf);pt_1=pt=0;while(v=buf[pt++]){if(v==' ')continue;pt_1=!pt_1;if(pt_1)u=v;elseg[mp[u]][mp[v]]=1;}rep(i,0,n)rep(j,0,n)rep(k,0,n)if(g[i][j]&&g[j][k])g[i][k]=1;if(cas++)putchar('\n');rep(i,0,n)rep(j,0,n)if(g[j][i])in[i]++;dfs(0);}}