http://poj.org/problem?id=1094
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24505 | Accepted: 8487 |
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.
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
【题解】:
拓扑排序到底就行,注意:
当遇到两个入度为0的情况,需要判断是否出现环
唉,不说了,代码写得真挫,乱七八糟,不过能AC
【code】:
1 /** 2 Judge Status:Accepted Memory:704K 3 Time:0MS Language:G++ 4 Code Lenght:2296B Author:cj 5 */ 6 7 #include<iostream> 8 #include<stdio.h> 9 #include<string.h> 10 #include<math.h> 11 #include<algorithm> 12 13 #define N 30 14 #define M N*N 15 using namespace std; 16 17 int map[N][N],mark[N],in[N],tin[N]; 18 char anstr[N]; 19 int n,flag; 20 21 void init() 22 { 23 memset(map,0,sizeof(map)); 24 memset(mark,0,sizeof(mark)); 25 memset(in,0,sizeof(in)); 26 memset(tin,0,sizeof(tin)); 27 } 28 29 int topCheck(int id) //拓扑排序 30 { 31 int i,cnt=0,pos=-1; 32 for(i=0;i<26;i++) 33 { 34 if(mark[i]&&!in[i]) 35 { 36 cnt++; 37 pos=i; 38 } 39 } 40 if(cnt>1) //当有多个入度为0的点 41 { 42 flag = 1; //标记出现多个入度为0的点的情况 43 in[pos] = -1; 44 anstr[id] = pos+'A'; 45 for(i=0;i<26;i++) 46 { 47 if(map[pos][i]) in[i]--; 48 } 49 return topCheck(id+1); 50 } 51 if(cnt==0) 52 { 53 for(i=0;i<26;i++) 54 { 55 if(mark[i]&&in[i]>0) 56 { 57 return 1; //入度都大于0,出现了环 58 } 59 } 60 if(id!=n||flag) return 0; //当排序到入度为0时,并且一直没有出现过多个入度为0的情况,则排序可以完成 61 anstr[id]='