首页 > 学院 > 开发设计 > 正文

poj 1094 Sorting It All Out(拓扑排序)

2019-11-06 07:35:07
字体:
来源:转载
供稿:网友

题目链接:http://poj.org/PRoblem?id=1094

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34551 Accepted: 12116

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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0

Sample Output

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

Source

East Central North America 2001

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:排下序,比较恶心的是问什么时候能排好序,或者什么时候构成环

解析:用栈进行维护即可

代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<cmath>#include<map>#include<vector>#include<stack>#define N 29using namespace std;const int INF = 0x3f3f3f3f;stack<int> s, e;vector<int> G[N];int IN[N], cnt, f;char ans[N];void init(int n){    memset(IN, 0, sizeof(IN));    for(int i = 0; i < n; i++) G[i].clear();}void solve(int n){    for(int i = 0; i < n; i++)    {        if(!IN[i])        {            s.push(i); e.push(i);        }    }    while(s.size())    {        if(s.size() > 1) f = 1;        int u = s.top(); s.pop();        ans[cnt++] = u + 'A';        for(int i = 0; i < G[u].size(); i++)        {            int v = G[u][i];            IN[v]--;            if(!IN[v])            {                s.push(v); e.push(v);            }        }    }    ans[cnt] = '/0';}int main(){    int u, v, n, k, ok;    char uch, vch;    while(scanf("%d%d", &n, &k), n + k)    {        int check = 0;        init(n);        for(int i = 0; i < k; i++)        {            scanf(" %c<%c", &uch, &vch);            if(!check)            {                u = uch - 'A'; v = vch - 'A';                G[u].push_back(v);                IN[v]++;                cnt = f = 0;                solve(n);                if(cnt != n)                {                    check = 1;                    ok = i + 1;                }                else if(cnt == n && !f)                {                    check = 2;                    ok = i + 1;                }                else if(i == k - 1 && f) check = 3;                while(e.size())                {                    u = e.top();                    e.pop();                    for(int j = 0; j < G[u].size(); j++)                    {                        v = G[u][j];                        IN[v]++;                    }                }            }        }        if(check == 1) printf("Inconsistency found after %d relations./n", ok);        else if(check == 2) printf("Sorted sequence determined after %d relations: %s./n", ok, ans);        else puts("Sorted sequence cannot be determined.");    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表