您的位置:首页 > 其它

POJ 1270 Following Orders (拓扑排序)

2013-04-24 16:58 453 查看
Following Orders

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 2971Accepted: 1134
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

/*
dfs实现的拓扑排序
因为输出结果要遵守字典序,所以首先将第一行输入的字符转换成整型,使字符和整数有
对应关系,然后进行升序排序。对于第二行的输入,则是构造图以及记录每个点的入度情
况。最后dfs的时候按顺序从小到大搜索,若当前点入度为0,说明它是在排序后的最左端
的,然后把和它相连的点的入度-1,若经过-1后产生入度为0的点,则对该点继续深搜。
注意每次搜完的时候回溯,还原图原来的样子。

204K 0MS
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>

using namespace std;

int idx;
int vt[30],deg[30];
bool cnt[30][30];
bool vis[30];
int temp[30];

void toposort(int len)
{
if(len == idx)
{
for(int j=0; j<idx; j++)
printf("%c",vt[temp[j]]+'a');
printf("\n");
return;
}
int i;
for(i=1; i<=idx; i++)
{
if(!vis[vt[i]] && !deg[vt[i]])
{
for(int j=1; j<=idx; j++)
{
if(cnt[vt[i]][vt[j]])
deg[vt[j]]--;
}
vis[vt[i]] = true;
temp[len] = i;
toposort(len+1);
for(int j=1; j<=idx; j++)
{
if(cnt[vt[i]][vt[j]])
deg[vt[j]]++;
}
vis[vt[i]] = false;
}
}
return ;
}

char dic[50];
char rel[200];
bool blank;

int main()
{
blank = false;
while(cin.getline(dic,50))
{
if(blank)
printf("\n");
blank = true;
memset(deg,0,sizeof(deg));
memset(vt,0,sizeof(vt));
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
idx = 0;
for(int i=0; dic[i] != '\0'; i++)
{
if(dic[i] != ' ')
vt[++idx] = dic[i] - 'a';
}
sort(vt+1,vt+1+idx);
cin.getline(rel,200);
int st = -1, ed = -1;
for(int i=0; rel[i] != '\0'; i++)
{
if(st == -1 && rel[i] != ' ' && rel[i] != '\n')
{
st = rel[i] - 'a';
continue;
}
if(st != -1 && rel[i] != ' ' && rel[i] != '\n')
{
ed = rel[i] - 'a';
cnt[st][ed] = true;
deg[ed] ++;
st = ed = -1;
}
}
toposort(0);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: