POJ-1094 Sorting It All Out

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.

题目大意:

给你n个字母和m个约束条件,让你给出最后能否找到一个序列符合。

解题思路:

拓扑排序。

这道题目= =非常的坑,比如如果你在前面以及确定了一个唯一的序列能够符合约束,后面再给出一个矛盾的条件是无效的。。。

如果排除这些之后,这道题目还是非常简单的。直接对于每次约束进行拓扑排序就可以了。

代码:

#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back

const int maxn = 30;

vector<int> ans, tot, vec[maxn];
int in[maxn], vis[maxn], tmp[maxn], cnt;

int toposort(int n){
    priority_queue<int, vector<int>, greater<int> > q;
    while(!q.empty()) q.pop();
    
    for(int i = 0; i < maxn; ++i) tmp[i] = in[i];
    
    int rec = 0;
    for(int i = 0; i < 26; ++i) 
        if(!tmp[i] && vis[i]) {
            q.push(i); 
            ++rec;
        }
    
    if(rec == 1) rec = 0;
    
    while(!q.empty()){
        int p = q.top();
        tot.pb(p); q.pop();
        
        int v, len = vec[p].size();
        for(int i = 0; i < len; ++i){
            v = vec[p][i];
            --tmp[v];
            if(!tmp[v]) q.push(v), ++rec;
        }
        
        if(rec == 1) rec = 0;
    }
    
    if( !rec && tot.size() == n) return 1;
    else if( tot.size() == cnt ) return 0;
    else return -1;
}
int main()
{
    // freopen("test.in", "r+", stdin);
    // freopen("test.out", "w+", stdout);
    
    char a, b;
    int n, m, x, y, res, flag;
    while(~scanf("%d%d", &n, &m)){
        if(n == 0 && m == 0) break;
        
        cnt = 0; flag = 0;
        memset(in, 0, sizeof(in));
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < maxn; ++i) vec[i].clear();
        
        for(int i = 1; i <= m; ++i) {
            scanf(" %c<%c", &a, &b);
            x = a - 'A';
            y = b - 'A';
            ++in[y];
            
            if(!vis[x]) ++cnt, vis[x] = 1;
            if(!vis[y]) ++cnt, vis[y] = 1;
            vec[x].pb(y);
            
            if(flag == 0){
                tot.clear();
                int k = toposort(n);
                if(k == 1) {
                    ans = tot;
                    flag = 1; res = i;
                }else if(k == -1){
                    flag = -1; res = i;
                }
            }
        }
        
        if(flag == 1){
            printf("Sorted sequence determined after %d relations: ", res);
            for(int i = 0; i < ans.size(); ++i){
                printf("%c", 'A'+ans[i]);
            }
            puts(".");
        }else if(flag == 0){
            printf("Sorted sequence cannot be determined.\n");
        }else{
            printf("Inconsistency found after %d relations.\n", res);
        }
    }
    return 0;
}


posted @ 2016-07-24 16:32  _Wilbert  阅读(155)  评论(0编辑  收藏  举报