利用DFS输出所有拓扑排序的可能,注意要按照字典序输出。
Following Orders
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 3045 | Accepted: 1164 |
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<sstream>
using namespace std;
#define INF 0x3f3f3f3f
int n;
int counts[30];
int g[30][30];
int que[30];
int vis[30];
void dfs(int k)
{
if(k==n)
{
for(int i=0;i<k;i++)
printf("%c",que[i]+'a');
printf("\n");
return;
}
for(int i=0;i<26;i++)
{
if(vis[i]) continue;
if(!counts[i])
{
for(int j=0;j<26;j++)
if(g[i][j]) counts[j]--;
que[k]=i;
vis[i]=1;
dfs(k+1);
vis[i]=0;
for(int j=0;j<26;j++)
if(g[i][j]) counts[j]++;
}
}
}
char str[200];
char u[20],v[20];
int main()
{
bool fg=0;
while(gets(str)!=NULL)
{
if(fg) printf("\n");
fg=1;
stringstream ss(str);
for(int i=0;i<26;i++)
counts[i]=INF;
n=0;
while(ss>>u)
n++,counts[u[0]-'a']=0;
gets(str);
stringstream sss(str);
memset(g,0,sizeof(g));
while(sss>>u>>v)
{
int a,b;
a=u[0]-'a';
b=v[0]-'a';
if(g[a][b]==0)
{
g[a][b]=1;
counts[b]++;
}
}
memset(vis,0,sizeof(vis));
dfs(0);
}
return 0;
}
本文介绍了一种基于深度优先搜索(DFS)实现的拓扑排序算法,并通过一个具体示例展示了如何找出所有符合约束条件的变量排序方案。该算法特别适用于解决依赖关系问题,如任务调度等场景。
286

被折叠的 条评论
为什么被折叠?



