Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28762 | Accepted: 9964 |
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
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 int n , m ; 6 bool map[30][30] ; 7 int in[30] ; 8 char st[1000][5] ; 9 char a[30] ; 10 11 int topo () 12 { 13 queue <int> q ; 14 int indegree[30] ; 15 int cnt = 0 ; 16 int k = 0 ; 17 int ans = -1 ;//record the condition that "cnt > 1" 18 while (!q.empty ()) 19 q.pop () ; 20 for (int i = 1 ; i <= n ; i++) 21 indegree[i] = in[i] ; 22 for (int i = 1 ; i <= n ; i++) { 23 if (in[i] == 0) { 24 q.push (i) ; 25 cnt++ ; 26 } 27 // printf ("%d " , in[i]) ; 28 } 29 if (cnt > 1) 30 ans = 1 ;//conditons are not satisfied 31 while (!q.empty ()) { 32 int temp = q.front () ; 33 a[k++] = 'A' + temp - 1 ; 34 q.pop () ; 35 cnt = 0 ; 36 for (int i = 1 ; i <= n ; i++) { 37 if (map[temp][i]) { 38 indegree[i]-- ; 39 if (indegree[i] == 0) { 40 cnt++ ; 41 q.push (i); 42 } 43 } 44 } 45 if (cnt > 1) 46 ans = 1 ;//conditons are not satisfied 47 } 48 if (k != n ) 49 return 0 ;//there is a circle 50 if (k == n && ans != 1) 51 return 2 ;//success 52 53 if (ans == 1) 54 return 1 ; 55 } 56 57 int main () 58 { 59 // freopen ("a.txt" , "r" , stdin) ; 60 int link ; 61 int flag ; 62 int success ; 63 while (~ scanf ("%d%d" , &n , &m)) { 64 if (n == 0 && m ==0) 65 break ; 66 getchar () ; 67 memset (in , 0 , sizeof(in)) ; 68 memset (map , 0 , sizeof(map)) ; 69 link = -1 ; 70 flag = -1 ; 71 success = -1 ; 72 for (int i = 0 ; i < m ; i++) 73 gets (st[i]) ; 74 75 for (int i = 0 ; i < m ; i++) { 76 int u = st[i][0] - 'A' + 1 ; 77 int v = st[i][2] - 'A' + 1 ; 78 // printf ("u = %d , v = %d " , u , v) ; 79 if (!map[u][v]) 80 in[v]++ ; 81 map[u][v] = 1 ; 82 83 flag = topo () ; 84 if (flag == 0) { 85 link = i + 1 ; 86 break ; 87 } 88 else if(flag == 2) { 89 success = i + 1 ; 90 break ; 91 } 92 // puts ("") ; 93 } 94 if (flag == 0) { 95 printf ("Inconsistency found after %d relations. " , link) ; 96 } 97 else if (flag == 1) { 98 puts ("Sorted sequence cannot be determined.") ; 99 } 100 else { 101 printf ("Sorted sequence determined after %d relations: " , success) ; 102 for (int i = 0 ; i < n ; i++) { 103 printf ("%c" , a[i]) ; 104 } 105 printf (". ") ; 106 } 107 } 108 return 0 ; 109 }
要一条条边测下来,not determined 肯定是最后一条边出来才能 判断 ,但在这之前若 先判断出了 success 或 inconsistency 就能结束了(一开始要把所有边先记录下来)
success只有 无环 & 同一时刻加入队列的点 == 1 时才能 成立