POJ-1094 Sorting It All Out

本文详细解析了一道关于拓扑排序的经典题目,介绍了如何通过不断输入边的方式进行拓扑排序并即时判断是否有环或确定唯一排序的过程。文章通过具体实例展示了算法实现细节,包括关键步骤和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K

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.

————————————————————集训6.2的分割线————————————————————

前言:整整做了一天。拓扑排序本身并没有那么难,但是姿势不对,就没有可行性了。因为这个并不是输入所有的边之后再让你排序的模板题,而是每次输入一条边,都要重新进行拓扑排序,并且要判断这一次是否产生了唯一解或者是否成环。

题意:很久才弄清楚。输入数字N,(要把A~Z的前N个字母进行拓扑排序)。然后输入M,(存在M条边)。

如果在某一次输入之后产生环,输出是哪一次。

如果确定了完整且唯一的拓扑序,输出在第几次得到,以及这个序列。

如果既没有产生环,而且不能确定唯一的拓扑序,输出cannot。(神题啊)

思路:每一次输入之后,进行一次拓扑排序。正确姿势如下:

For(i = 0 ~ n) {//对每个顶点
	For(k = 0 ~ n) {
		找到一个入度为0且未曾访问的点cur;
	}
	For(与该点相连所有的边) {
		入度--;
	}
	vis[cur] = true;
}
这道神题的关键点是做判断的优先级。一定要先判断是否成环,然而想要判断是否成环则要等到在剩下的点中找不到入度为0的点的时候。而且初次判断结束后,不必再重新判断(Fu k)。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int N = 30, M = N*N;
bool vis[N], confuse, go_on;
int n, m, indeg[N], degree[N];
vector <int> G[N], ans;

void init()
{
	ans.clear();
	memcpy(degree, indeg, sizeof(indeg));
	memset(vis, 0, sizeof(vis));
	confuse = false;
}

int Topsort()
{
	init();
	for(int k = 0; k < n; k++) {
		int zero = 0, alph = 0, cur;
		for(int i = 0; i < n; i++) {
			if(vis[i]) continue;
			if(!degree[i]) {
				cur = i;
				zero++;
			}
		}
		if(zero) {
			for(int i = 0; i < G[cur].size(); i++) {
				degree[G[cur][i]]--;
			}
			vis[cur] = true;
			ans.push_back(cur);
		}
		if(zero == 0) 
			return 1;//最高优先级别,但是必须在所有0入度点访问完之后才能肯定
		if(zero > 1)
			confuse = true;//多个入度为0,继续访问入度为0的点
	}
	if(confuse) return 2;
	return 0;
}

void PR(int ret, int rela)
{
	if(ret == 1) {
		go_on = false;
		printf("Inconsistency found after %d relations.\n", rela);
	}
	else if(ret == 0) {
		go_on = false;
		printf("Sorted sequence determined after %d relations: ", rela);
		for(int j = 0; j < n-1; j++)
			printf("%c", ans[j]+'A');
		printf("%c.\n", ans[n-1]+'A');
	}
	else if(rela == m && ret == 2) {
		puts("Sorted sequence cannot be determined.");
	}
}

int main()
{
#ifdef J_Sure
	freopen("1094.in", "r", stdin);
	//freopen(".out", "w", stdout);
#endif
	while(scanf("%d%d", &n, &m),n) {
		for(int i = 0; i < N; i++) 
			G[i].clear();
		memset(indeg, 0, sizeof(indeg));
		go_on = true;
		char a, b; int u, v;
		int ret;
		for(int i = 1; i <= m; i++) {
			scanf(" %c<%c", &a, &b);
			u = a - 'A'; v = b - 'A';
			if(go_on) {
				indeg[v]++;
				G[u].push_back(v);
				ret = Topsort();
				PR(ret, i);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值