POJ1094[有向環 拓撲排序]

Candy?發表於2016-09-12
Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33184   Accepted: 11545

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


因為要求幾個relation,所以沒加一個進行一次topoSort
一旦遇到有環或者唯一有序就sign=1,後面的關係一定要讀完(後果自負),如果最後sign==0說明沒確定順序
要列印方案,可以用char  topo[]來儲存,然而輸出topo[]比一個個輸出慢了16MS,並且還忘初始化了WA好幾次
//
//  main.cpp
//  poj1094
//
//  Created by Candy on 9/11/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=30;
int n,m,ind[N],ch[N][N];
char tmp[5],topo[N];
int q[N];
int topoSort(){
    int flag=2;//only sorted
    int indt[N];
    for(int i=1;i<=n;i++) indt[i]=ind[i];
    for(int i=1;i<=n;i++){//printf("%d %d %d \n",i,ind[i],ch[i][0]);
        int zero=0,u=0;
        for(int j=1;j<=n;j++) if(indt[j]==0) zero++,u=j;
        if(zero==0) return 1;//circle
        if(zero>1) flag=3;//no sorted
        indt[u]--;
        topo[i-1]=(char)u+'A'-1;
        q[i-1]=u;
        for(int j=1;j<=ch[u][0];j++)
            indt[ch[u][j]]--;
    }
    return flag;
}
int main(int argc, const char * argv[]) {
    while(cin>>n>>m){
        if(n==0&&m==0) break;
        memset(ch,0,sizeof(ch));
        memset(ind,0,sizeof(ind));
        memset(topo,0,sizeof(topo));
        int sign=0;
        
        for(int i=1;i<=m;i++){
            scanf("%s",tmp);
            if(sign) continue;
            int x=tmp[0]-'A'+1,y=tmp[2]-'A'+1;
            ind[y]++;
            ch[x][++ch[x][0]]=y;
            int flag=topoSort();
            if(flag==1){sign=1;printf("Inconsistency found after %d relations.\n",i);}
            if(flag==2){
                sign=1;printf("Sorted sequence determined after %d relations: %s.\n",i,topo);
//                printf("Sorted sequence determined after %d relations: ",i);
//                for(int j=0;j<n;j++)
//                    printf("%c",q[j]+'A'-1);
//                printf(".\n");
//                sign=1;
            }
        }
        if(!sign) printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

 

 

相關文章