程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 1094 Sorting It All Out(拓撲排序)

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

編輯:C++入門知識

題目:
Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21532   Accepted: 7403
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.

分析:
這題典型的拓撲排序,但是有點變化。
題目樣例的三種輸出分別是:
1. 在第x個關系中可以唯一的確定排序,並輸出。
2. 在第x個關系中發現了有回環(Inconsisitency矛盾)
3.全部關系都沒有發現上面兩種情況,輸出第3種.

那麼對於給定的m個關系,一個個的讀進去,每讀進去一次就要進行一次拓撲排序,如果發現情況1和情況2,那麼就不用再考慮後面的那些關系了,但是還要繼續讀完後面的關系(但不處理)。如果讀完了所有關系,還沒有出現情況1和情況2,那麼就輸出情況3.
拓撲排序有兩種方法,一種是算法導論上的,一種是用貪心的思想,這題用貪心的思想做更好。

貪心的做法:
1. 找到所有入度為0的點, 加入隊列Q
2.取出隊列Q的一個點,把以這個點為起點,所有它的終點的入度都減1. 如果這個過程中發現經過減1後入度變為0的,把這個點加入隊列Q。
3.重復步驟2,直到Q為空。

這個過程中,如果同時有多個點的入度為0,說明不能唯一確定關系。
如果結束之後,所得到的經過排序的點少於點的總數,那麼說明有回環。


題目還需要注意的一點:如果邊(u,v)之前已經輸入過了,那麼之後這條邊都不再加入。


代碼:
[cpp] 
#include<cstdio> 
#include<cstring> 
#include<vector> 
#include<queue> 
using namespace std; 
const int N = 105; 
int n,m,in[N],temp[N],Sort[N],t,pos, num; 
char X, O, Y; 
vector<int>G[N]; 
queue<int>q; 
 
void init(){ 
    memset(in, 0, sizeof(in)); 
    for(int i=0; i<=n; ++i){ 
        G[i].clear(); 
    } 

 
inline bool find(int u,int v){ 
    for(int i=0; i<G[u].size(); ++i) 
        if(G[u][i]==v)return true; 
    return false; 

 
int topoSort(){ 
    while(!q.empty())q.pop(); 
    for(int i=0; i<n; ++i)if(in[i]==0){ 
            q.push(i); 
    } 
    pos=0; 
    bool unSure=false; 
    while(!q.empty()){ 
        if(q.size()>1) unSure=true; 
        int t=q.front(); 
        q.pop(); 
        Sort[pos++]=t; 
        for(int i=0; i<G[t].size(); ++i){ 
            if(--in[G[t][i]]==0) 
                q.push(G[t][i]); 
        } 
    } 
    if(pos<n) return 1; 
    if(unSure)  return 2; 
    return 3; 

 
int main(){ 
    int x,y,i,flag,ok,stop; 
    while(~scanf("%d%d%*c",&n,&m)){ 
        if(!n||!m)break; 
        init(); 
        flag=2; 
        ok=false; 
        for(i=1; i<=m; ++i){ 
            scanf("%c%c%c%*c", &X,&O,&Y); 
            if(ok) continue; // 如果已經判斷了有回環或者可唯一排序,不處理但是要繼續讀 
            x=X-'A', y=Y-'A'; 
            if(O=='<'&&!find(y,x)){ 
                    G[y].push_back(x); 
                    ++in[x]; 
            } 
            else if(O=='>'&&!find(x,y)){ 
                    G[x].push_back(y); 
                    ++in[y]; 
            } 
            // 拷貝一個副本,等下用來還原in數組 
            memcpy(temp, in, sizeof(in));  
            flag=topoSort(); 
            memcpy(in, temp, sizeof(temp)); 
            if(flag!=2){ 
                stop=i; 
                ok=true; 
            } 
        } 
        if(flag==3){ 
            printf("Sorted sequence determined after %d relations: ", stop); 
            for(int i=pos-1; i>=0; --i) 
                printf("%c",Sort[i]+'A'); 
            printf(".\n"); 
        } 
        else if(flag==1){ 
            printf("Inconsistency found after %d relations.\n",stop); 
        } 
        else{ 
            printf("Sorted sequence cannot be determined.\n"); 
        } 
    } 
    return 0; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved