您的位置:首页 > 其它

poj-1270 Following Orders(拓扑排序)

2014-08-03 20:14 253 查看
Following Orders

Time Limit:1000MS Memory Limit:10000KB
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

题意:输入两行,第一行给n个字母,第二行给出x<y(y在x后面输出)的拓扑关系,让你输出可能输出的顺序。

思路:根据输入信息构造有向图,ans[26]保存a~z的入度,go[i]=1表示第一行输入过i节点,s[x][0]表示x<y关系中y的个数,

s[x][1~s[x][0]]保存y。

dsf,遍历a~z,输出符合ans[x]==0的,然后与x存在x<y关系的ans[y]--。

输入用sstream类库读取数据的方法。

#include<iostream>
#include<sstream>
#include<string>
#include<stdio.h>
using namespace std;
int s[26][26];
int n,ans[26],go[26];
void dfs(int v,string s1)
{
	if(v==n){
		printf("%s\n",s1.c_str());
		return;
	}
	for(int i=0;i<26;i++)
	{
		if(go[i]&&!ans[i]){
			string s2=s1+char(i+'a');
			go[i]=0;
			for(int j=1;j<=s[i][0];j++)
				ans[s[i][j]]--;
			dfs(v+1,s2);
			go[i]=1;
			for(int k=1;k<=s[i][0];k++)
				ans[s[i][k]]++;
		}
	}
}
int main()
{
	string line,line2;
	int i,m,x=0;
	char c,c2;
	while(getline(cin,line))
	{
		x++;
		if(x!=1) printf("\n");
		getline(cin,line2);
		memset(ans,0,sizeof(ans));
		memset(s,0,sizeof(s));
		memset(go,0,sizeof(go));
		stringstream ss(line);
		stringstream sss(line2);
		for(n=0;ss>>c;n++)
			go[c-'a']=1;
		for(m=0;sss>>c;m+=2)
		{
			sss>>c2;
			s[c-'a'][0]++;
			s[c-'a'][s[c-'a'][0]]=c2-'a';
			ans[c2-'a']++;
		}
		string s1;
		dfs(0,s1);
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: