Following Orders
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4436 | Accepted: 1791 |
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.
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.
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.
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
思路:拓扑排序;我们知道拓扑排序中有很多点是可以交换位置的,因为他们之间没有先后的约束,所以我们可以DFS求所有符合要求的拓扑排序输出
即可,注意要按字典序。
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<queue> 6 #include<vector> 7 using namespace std; 8 char aa[100]; 9 char bb[300]; 10 int num1[100]; 11 int num2[300]; 12 int an[300]; 13 int flag[300]; 14 int dd[300]; 15 vector<int>vec[300]; 16 void top(int n,int ans,int k); 17 int main(void) 18 { 19 int i,j,k,p,q; 20 while(gets(aa)!=NULL) 21 { memset(an,0,sizeof(an)); 22 memset(flag,0,sizeof(flag)); 23 for(i=0;i<300;i++) 24 vec[i].clear(); 25 gets(bb);int ans=0; 26 for(i=0;aa[i]!='