【拓扑排序】Sorting It All Out POJ - 1094

拓扑排序

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.

分析

此题分为三种情况
第一种情况:
拓扑排序后的拓扑序列里存在n个点,并且这n个点构成的树的深度为n,说明每个点之间的关系都可以确定。
如果把拓扑排序后的序列看成一棵树,只有在树高为n时,才能确定这n个点之间的关系,每个点都从父节点指向子节点,如果树高小于n,那么肯定有一层的结点数量大于等于2,此时这一层的结点之间的关系就不能确定了,但是这并不能确定是第三种情况,只有当给出的m种情况都用上也不能确定顺序时,才是第三种情况。
第二种情况:
出现环。如果对一个有x个点的图进行拓扑排序,排序后的序列中点的个数不够x,那么说明这个图中有环,也就是出现了矛盾。因为拓扑排序是,只有入度为0才能入队继续BFS,如果存在环,那么环上的每个点的入度都不可能减为0。
第三种情况:
如果给出的m个条件都不能满足前两种情况,说明不能确定n个点的顺序。

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

using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 30;
int n, m;
vector<int> e[N];//存边
bool book[N];//标记字母是否出现
bool vis[N];//是否在拓扑序列中
struct node {
    int u, step;

    explicit node(int u = 0, int step = 0) : u(u), step(step) {}
};

int sum;//最大深度
int in[N];//入度
vector<int> T;//存拓扑序列

void toposort() {
    sum = 0;
    T.clear();
    queue<node> q;
    memset(vis, false, sizeof(vis));
    memset(in, 0, sizeof(in));
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < e[i].size(); ++j) {
            in[e[i][j]]++;
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (in[i] == 0 && book[i]) {
            q.push(node(i, 1));
        }
    }
    while (!q.empty()) {
        int u = q.front().u;
        int step = q.front().step;
        q.pop();//出队很重要!!!
        T.push_back(u);
        for (int i = 0; i < e[u].size(); ++i) {
            int v = e[u][i];
            in[v]--;
            if (in[v] == 0) {
                q.push(node(v, step + 1));
                sum = max(sum, step + 1);
            }
        }
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF && n != 0 && m != 0) {
        int flag = 0, k = 0;
        char x, y;
        for (int i = 0; i < N; ++i)e[i].clear();
        memset(book, false, sizeof(book));
        for (int i = 1; i <= m; ++i) {
            scanf(" %c<%c", &x, &y);
            e[x - 'A' + 1].push_back(y - 'A' + 1);
            book[x - 'A' + 1] = book[y - 'A' + 1] = true;
            if (flag == 0) {
                int cnt = 0;
                for (int j = 1; j <= n; ++j) {
                    if (book[j])cnt++;
                }
                toposort();
                if (sum == n) { //最大深度为n说明已经确定n个点的关系
                    flag = 1;
                    k = i;
                } else if (T.size() != cnt) {   //拓扑序列中点的个数与图中的点的个数不相等 说明有环
                    flag = 2;
                    k = i;
                }
            }
        }
        if (flag == 1) {
            printf("Sorted sequence determined after %d relations: ", k);
            for (int i = 0; i < T.size(); ++i) printf("%c", 'A' + T[i] - 1);
            printf(".\n");
        } else if (flag == 2) {
            printf("Inconsistency found after %d relations.\n", k);
        } else {    //前两种情况都不满足 说明不能确定
            printf("Sorted sequence cannot be determined.\n");
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值