UVA 124 & POJ 1270 Following Orders(拓扑排序)

2020-12-13 06:18

阅读:433

标签:algorithm   图论   拓扑排序   

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60

http://poj.org/problem?id=1270

Following Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3806   Accepted: 1507

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


For example, given the constraints x

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
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

Source

Duke Internet Programming Contest 1993,uva 124


题意:

输入有两行,第一行给出若干出现的字母,第二行给出若干对关系x y,表示x

分析:

按字典序输出所有的拓扑序,和POJ 1128 & ZOJ 1083的方法一样,回溯求解即可,详情请戳这里: POJ 1128 & ZOJ 1083 Frame Stacking (拓扑排序)

这题的输入是比较恶心的,要注意写得鲁棒些。


/*
 *
 *	Author	: 	fcbruce
 *
 *	Date	:	2014-08-10 15:22:35 
 *
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 
#define maxn 

using namespace std;

bool appear[30],pending[30],G[30][30];
int deg[30],st[30];
int n;

void write()
{
	for (int i=0;i

UVA 124 & POJ 1270 Following Orders(拓扑排序),搜素材,soscw.com

UVA 124 & POJ 1270 Following Orders(拓扑排序)

标签:algorithm   图论   拓扑排序   

原文地址:http://blog.csdn.net/u012965890/article/details/38470271


评论


亲,登录后才可以留言!