您的位置:首页 > 其它

POJ 1094-Sorting It All Out 拓扑排序

2010-08-26 21:14 295 查看
题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=1094



解题报告:



每添加一条边,就检查一次是否可以对排序顺序进行确定。



每一次检查时,对已形成的图进行深搜,如果在深搜时遇到了灰色结点,就说明图中存在反边,即Inconsistency found;深搜的同时将结点按照访问结束的顺序放入队列Q1中,最后获得的Q1就是拓扑排序的结果,设为ABCD...,如果对队列中的连续的亮点,它们的关系是确定的,即存在A->B->C->D->...,则它们的关系就是确定的,否则,就是关系不能确定。如果关系无法确定,就添加下一条边,继续判断。



#include <iostream>
#include <queue>
#include <stack>
//#include <fstream>
using namespace std;

int V[26]; //每个点的邻接点的个数
int Adj[26][26];//邻接点 Adj[i][j]=1,代表i的邻接点中有j
int color[26]; //每个点在DFS中的访问状态
int n,m;
queue<int> Q1; //拓扑排序中的队列
stack<int> Q2;
bool check;

void dfs_visit(int);

int dfs()
{
	check=false;
	while(!Q1.empty())
		Q1.pop(); //清空队列
	while(!Q2.empty())
		Q2.pop();
	for(int i=0;i<n;i++)
		color[i]=0;
	for(int i=0;i<n;i++)
	{
		if(color[i]==0)
			dfs_visit(i);
	}
	if(check==true)
		return 2; //Inconsistency被发现
	int u=Q1.front();
	Q1.pop();
	Q2.push(u);
	while(!Q1.empty())
	{
		int v=Q1.front();
		Q2.push(v);
		Q1.pop();
		if(Adj[v][u]!=1)
			return 1;//暂时无法发现其中的关系
		u=v;
	}
	return 0; //关系被determined了
}

void dfs_visit(int u)
{
	if(check)
		return;
	color[u]=1;
	for(int i=0;i<n;i++)
	{
		if(Adj[u][i]==1)//是邻接点
		{
			if(color[i]==0)
				dfs_visit(i);
			else if(color[i]==1) //找到反边,Inconsisitency
			{
				check=true;
				break;
			}
		}
	}
	if(check)
		return;
	else
	{
		color[u]=2;
		Q1.push(u);
	}
}

int main()
{
//	ifstream cin("test.txt");
	while((cin >> n >> m) && !(n==0 && m==0))
	{
		for(int i=0;i<n;i++)
		{
			V[i]=0;
			for(int j=0;j<n;j++)
				Adj[i][j]=0;
		} //一些初始化操作
		bool judge=true; //继续判断的标准
		int d;
		int cnt;
		for(int i=0;i<m;i++)
		{
			char t1,t2,temp;
			cin >> t1 >> temp >> t2;
			if(judge)
			{	
				V[t1-'A']++;
				Adj[t1-'A'][t2-'A']=1;
				d=dfs();
				if(d==0)
				{
					cnt=i+1;
					judge=false;
				}
				else if(d==2)
				{
					cnt=i+1;
					judge=false;
				}
				else if(i==m && d==1)
				{
					judge=false;
				}
			}
		}
		if(d==0)
		{
			printf("Sorted sequence determined after %d relations: ",cnt);
			while(!Q2.empty())
			{
				cout << (char)(Q2.top()+'A');
				Q2.pop();
			}
			cout << "." << endl;
		}
		else if(d==2)
			printf("Inconsistency found after %d relations./n",cnt);
		else if(d==1)
			cout << "Sorted sequence cannot be determined." << endl;
	}
}




附录:



Sorting It All Out

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 13864Accepted: 4616
Description
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
Input
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
Output
For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.

Sample Input
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

Source
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: