POJ 1094-Sorting It All Out(元素大小關係-拓撲排序)

kewlgrl發表於2016-07-30
Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32635   Accepted: 11345

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.

Source


題目意思:

由一些不同元素組成的升序序列可以使用若干個小於號將所有的元素按從小到大的順序排列起來的序列。
例如,排序後為ABCD,這說明A<B,B<C,C<D.
給定一組形如A<B的表示式,是判斷是否存在一個有序序列。

解題思路:

拓撲排序。
先建圖用鄰接矩陣存邊,再用一個陣列記錄下所有的入邊度數。
分三種情況討論:
圖中存在環
圖中不存在環,拓撲排序後元素個數小於N,說明無法確定全部元素的大小關係;
圖中不存在環,拓撲排序後元素個數等於N,直接輸出拓撲序列即為正確的大小關係。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 30//開太大會超記憶體
int n,m;
char top[MAXN];
int in[MAXN],temp[MAXN],e[MAXN][MAXN];
bool vis[MAXN];

int topsort()
{
    int i,j,k,cnt,cto=0;
    int flag=1;
    memcpy(temp,in,sizeof(in));
    for(k=0; k<n; ++k)
    {
        cnt=0;
        for(i=0; i<n; ++i)
            if(temp[i]==0)
            {
                j=i;
                ++cnt;//入度為0的點的個數
            }
        if(cnt==0) return 0;//有環
        if(cnt>1) flag=-1;//無序
        for(i=0; i<n; ++i)
            if(e[j][i]==1)//有邊
                --temp[i];//刪去當前度為0的點的出邊
        top[cto++]=j+'A';//拓撲序列
        temp[j]=-1;
    }
    return flag;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/x/read.txt","r",stdin);
    freopen("G:/x/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>n>>m&&n!=0&&m!=0)
    {
        memset(vis,false,sizeof(vis));
        memset(in,0,sizeof(in));
        memset(e,0,sizeof(e));
        memset(top,0,sizeof(top));//注意這些陣列一定要清除乾淨
        char a,b,c;
        int t=0;
        bool istop=false;//是否判斷完畢
        for(int i=0; i<m; ++i)
        {
            cin>>a>>c>>b;
            if(istop) continue;//判斷完之後只讀入不再處理
            e[int(a)-'A'][int(b)-'A']=1;//存邊
            ++in[int(b)-'A'];//入邊
            t=topsort();//拓撲排序
            if(t==0)
            {
                cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
                istop=true;
            }
            else if(t==1)
            {
                cout<<"Sorted sequence determined after "<<i+1<<" relations: "<<top<<"."<<endl;
                istop=true;
            }
        }
        if(!istop) cout<<"Sorted sequence cannot be determined."<<endl;
    }
    return 0;
}
/**
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
**/

分享幾組測試資料:

4 6
D<B
D<A
D<C
C<A
C<B
A<B
10 40
C<I
E<G
A<J
F<B
D<E
F<D
C<B
E<H
G<I
D<B
C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
I<B
P<K
K<E
N<G
C<F
M<T
S<B
A<K
S<E
A<F
A<T
P<G
R<E
D<I
F<K
I<S
G<J
H<F
G<S
L<F
P<I
M<L
G<I
N<P
B<T
L<P
J<K
C<H
B<K
A<I
G<E
G<T
N<M
M<B
H<R
D<J
M<E
F<G
Q<M
D<S
H<B
N<J
Q<E
G<O
C<N
H<A
P<T
Q<B
L<S
R<S
N<L
L<G
Q<R
F<O
A<R
L<E
Q<P
E<T
M<R
M<S
L<O
A<L
R<P
H<J
L<K
M<P
A<M
L<T
R<T
L<R
S<J
N<E
F<I
A<D
D<F
Q<O
J<O
O<E
C<L
A<B
N<K
N<F
G<K
D<T
N<I
H<L
M<I
L<J
0 0

執行結果:

Sorted sequence determined after 6 relations: DCAB.
Sorted sequence determined after 29 relations: CFDAEBHGJI.
Sorted sequence cannot be determined.
Inconsistency found after 48 relations.
Sorted sequence determined after 318 relations: GMNVKCHFOBRJDZLPXAYSEIWQTU.
Sorted sequence determined after 25 relations: ABCDEFGHIJKLMNOPQRSTUVWXYZ.
Sorted sequence determined after 7 relations: BADCE.
Sorted sequence determined after 158 relations: HNAMGIPCTBJFRLEODKSQ.
Inconsistency found after 35 relations.
Sorted sequence cannot be determined.
Sorted sequence determined after 7 relations: ABDEC.
Sorted sequence cannot be determined.
Sorted sequence determined after 11 relations: ACDBFE.
Sorted sequence cannot be determined.
Sorted sequence cannot be determined.
Sorted sequence determined after 179 relations: CHQNAMDLRFPGISBJOKET.


相關文章