1. 程式人生 > >Sorting It All Out

Sorting It All Out

tar cte obj urn sta ios printf stream malle

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.

題意是問是否存在唯一的序列,如果不存在,要麽是不唯一,要麽是直接不存在,也就是存在環,拓撲排序,每次讀入都進行一次排序,然後通過返回值判斷。

代碼:
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define inf 10001
using namespace std;
int n,m;
int mp[26][26];///記錄兩個點是否存在前後順序
int ok[26],unsure;///ok記錄某個點前面是否有點 unsure記錄順序是否唯一
char ans[27],x,y,ch;///ans記錄唯一的序列 index為下標 flag記錄第幾步中斷
int index,flag;
int topsort()///拓撲
{
    queue<int> q;
    unsure = 0;///初始
    index = 0;///初始
    int vis[26] = {0};///當vis的值和ok相同就相當於這個點前面已經沒有點了,可以進入ans序列
    for(int i = 0;i < n;i ++)
    {
        if(vis[i] == ok[i])q.push(i);
    }
    if(q.empty())return -1;///如果不存在這樣的點 說明存在環
    while(!q.empty())
    {
        if(q.size() > 1)
        {
            unsure = 1;///存在多個點無前後確定關系
        }
        ans[index ++] = q.front() + A;///進入ans序列
        for(int i = 0;i < n;i ++)
        {
            if(mp[q.front()][i])
            {
                vis[i] ++;///i前面的點少了一個 之所以用vis,是維護ok和mp數組不變保證每次都是完整的拓撲
                if(vis[i] == ok[i])q.push(i);
                else if(vis[i] > ok[i])return -1;//有環
            }
        }
        q.pop();
    }
    return 1;
}
int main()
{
    while(cin>>n>>m&&(n + m))
    {
        index = 0;
        flag = -1;
        int d = 0;
        memset(mp,0,sizeof(mp));
        memset(ok,0,sizeof(ok));
        for(int i = 0;i < m;i ++)
        {
            cin>>x>>ch>>y;
            if(!mp[x - A][y - A])
            {
                if(ch == <)
                {
                    mp[x - A][y - A] = 1;
                    ok[y - A] ++;
                }
                else if(ch == >)
                {
                    mp[y - A][x - A] = 1;
                    ok[x - A] ++;
                }
            }
            if(d == 1 || d == -1)continue;
            d = topsort();
            if(d == 1)///排序進行完
            {
                if(index == n)///n個點排好序了
                {
                    if(unsure == 0)flag = i + 1,ans[index] = \0;///序列唯一
                    else d = 0;//不唯一
                }
                else///有環
                {
                    d = -1,flag = i + 1;
                }
            }
            else if(d == -1)flag = i + 1;///有環
        }
        if(!d)printf("Sorted sequence cannot be determined.\n");
        else if(d == 1)printf("Sorted sequence determined after %d relations: %s.\n",flag,ans);
        else printf("Inconsistency found after %d relations.\n",flag);
    }
}

Sorting It All Out