poj1094

拓扑排序,用个数组模拟水一水也是可以。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

#ifndef ONLINE_JUDGE
#include <windows.h>
#endif

using namespace std;

const int MAX = 30;
int G[MAX][MAX];
bool vis[MAX][MAX];
int n, m;

int go(int f, int t) {
	if (vis[f][t]) return G[f][t] == 1 ? 1 : -1;
	vis[f][t] = true; //加个判重,否则递归递归栈过深,会TLE 

	if (G[f][t] == -1) G[f][t] = 1;
	else if (G[f][t] == 0) return -1;
	if (G[t][f] == -1) G[t][f] = 0;
	else if (G[t][f] == 1) return -1;
	
	for (int i = 0; i < n; ++i) {
		if (G[t][i] == 1) {
			if (go(f, i) == -1) return -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (G[i][f] == 1) {
			if (go(i, t) == -1) return -1;
		}
	}
	return 1;
}

bool check(void) {
	int s = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (G[i][j] == 1) ++s;
		}
	}
	return s == n * (n - 1) / 2;
}

void getSort(void) {
	int rank[MAX] = {0};
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (G[i][j] == 1) rank[i]++;
		}
	}
	for (int pa = n - 1; pa >= 0; --pa) {
		for (int j = 0; j < n; ++j) {
			if (rank[j] == pa) {
				putchar(j + 'A');
				break;
			}
		}
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("r.in", "r", stdin);
#endif
	while (scanf(" %d %d", &n, &m) && (n | m)) {
		memset(G, -1, sizeof(G));
		memset(vis, false, sizeof(vis));
		char str[10];
		int flag = -1, step;
		for (int i = 1; i <= m; ++i) {
			scanf(" %s", str);
			if (flag != -1) continue;
			int x = str[0] - 'A', y = str[2] - 'A';
			if (go(x, y) == -1) {
				flag = 0;
				step = i;
			}
			if (check()) {
				flag = 1;
				step = i;
			}
			//Debug();
		}
		if (flag == -1) puts("Sorted sequence cannot be determined.");
		else if (flag == 0) {
			printf("Inconsistency found after %d relations.\n", step);
		} else {
			printf("Sorted sequence determined after %d relations: ", step);
			getSort();
			puts(".");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值