MENU

POJ1094 Sorting It All Out(拓扑排序)

2018 年 07 月 20 日 • 阅读: 1318 • 图论阅读设置

Sorting It All Out

  • 总时间限制: 1000ms
  • 内存限制: 65536kB

描述

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 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.

输出

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.

样例输入

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

样例输出

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

来源

East Central North America 2001


链接

http://bailian.openjudge.cn/practice/1094?lang=en_US【POJ同题号】

题意

给定一个字符序列,我们可以确定每个字符的大小顺序,比如给定ABCD,有A<B、B<C、C<D。现在,给定多组字符大小顺序的关系式,问能否通过这些关系式确定一个有序序列。

  • 如果能确定有序序列,输出最早能够确定顺序的关系式的数量以及该序列
  • 如果出现冲突,输出最早能够确定冲突的关系式的数量
  • 如果不能确定也不存在冲突,输出无法得到

题解

每来一个小于关系就进行一次拓扑排序 ,直到出现冲突(也就是出现了环)或者已经能确定顺序,当结果已经确定时,后面的小于关系也就没有必要处理了  因此可以用一个flag标记结果是否已经确定。

注意判拓扑序列不唯一时应放在输入完毕后,以及不能确定的条件。具体实现看代码。

参考了ddwust题解

代码

StatusAccepted
Time1ms
Memory128kB
Length1779
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 30;
int indegree[maxn], _indegree[maxn];
int n, m;
vector <int> E[maxn];
char ans[maxn];
queue <int> Q;

void init()
{
    for (int i = 0; i < n; ++i)
    {
        E[i].clear();
        indegree[i] = 0;
    }
}

int Topsort()
{
    int size = 0, ret = 1;
    memcpy(_indegree, indegree, sizeof(_indegree));
    for (int i = 0; i < n; ++i)
    {
        if (_indegree[i] == 0)
            Q.push(i);
    }
    while (!Q.empty())
    {
        if (Q.size() > 1) // 当前状态下有多个入度为0的节点,选择的顺序不能确定
            ret = 0;
        int u = Q.front();
        Q.pop();
        ans[size++] = char(u + 'A');
        for (int i = 0; i < E[u].size(); ++i)
        {
            int v = E[u][i];
            if ((--_indegree[v]) == 0)
                Q.push(v);
        }
    }
    if (size < n) // 出现环
        return -1;
//    for (int i = 0; i < n; ++i) // 判断环,同上
//        if (_indegree[i] != 0)
//            return -1;
    ans[size] = '\0'; // 不加会报错
    return ret;
}

int main()
{
    char s[10];
    int u, v;
    while (scanf("%d%d", &n, &m) != EOF && n && m)
    {
        init();
        int flag = 0;
        for (int i = 0; i < m; ++i)
        {
            scanf("%s", s);
            if (flag) // 如果已经判断完成只需继续输入即可
                continue;
            u = s[0] - 'A', v = s[2] - 'A';
            E[u].push_back(v);
            indegree[v]++;
            flag = Topsort();
            if (flag == 1)
                printf("Sorted sequence determined after %d relations: %s.\n", i + 1, ans);
            else if (flag == -1)
                printf("Inconsistency found after %d relations.\n", i + 1);
        }
        if (!flag)
            printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

注意C语言的字符数组,直接输出的话得加结束标记\0


The end.
2018-07-20 星期五

模板

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1010;
int vis[maxn];
vector <int> E[maxn];
vector <int> V;
int n, m;

bool dfs(int u)
{
    vis[u] = -1; // 当前
    for (int i = 0; i < E[u].size(); ++i)
    {
        int v = E[u][i];
        if (vis[v] == -1) // 出现环
            return false;
        else if (!vis[v])
            dfs(v);
    }
    V.push_back(u);
    vis[u] = 1;
    return true;
}

bool TopSort()
{
    for (int i = 1; i <= n; ++i)
        if (!vis[i])
        {
            if (!dfs(i))
                return false;
        }
    return true;
}


int main()
{

    scanf("%d%d", &n, &m);
    int u, v;
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d", &u, &v);
        E[u].push_back(v);
    }
    if (TopSort())
    {
        for (int i = V.size() - 1; i >= 0; --i)
            printf("%d ", V[i]);
    }
    else
        printf("No\n");
}

/*
/Users/taifu/Codes/CPP/cmake-build-debug/CPP
13 15
1 2
1 6
1 7
3 1
3 4
4 6
6 5
7 4
7 10
8 7
9 8
10 11
10 12
10 13
12 13
9 8 3 1 7 10 12 13 11 4 6 5 2
Process finished with exit code 0
*/