Sorting It All Out

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24354   Accepted: 8417

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.
/*
此题要注意优先级,当在输入的过程中出现环或拓扑排序成功时可以直接输出,但当前驱结点不唯一时不能直接输出,因为后面的输入可能导致出现环,这时候就应该输出矛盾而不是拓扑序列不唯一了,所以判拓扑序列不唯一时应放在输入完毕后;wa了好多次,有想哭的冲动;
*/
 1 #include<stdio.h>
 2 #include<string.h>
 3 int n,m,cnt;
 4 int degree[30];
 5 bool map[30][30],vis[30];
 6 char topo[30];
 7 
 8 int toposort()
 9 {
10     memset(vis,0,sizeof(vis));
11     int indegree[30];//函数内部设一个记录入度的数组,尝试着进行拓扑,因为记录入度的外部数组不能随意更改;
12     int i,j,k,m;
13     for(i = 0; i < 26; i++)
14         indegree[i] = degree[i];
15     cnt = 0;
16     m = 0;//判断是否有多个前驱结点,注意初始化放在外循环外面;
17     int count = 0;//记录前驱结点的个数;
18     for(i = 0; i < n; i++)
19     {
20 
21         for(j = 0; j < n; j++)
22         {
23             if(indegree[j] == 0 && !vis[j])
24             {
25                 vis[j] = 1;//把该入度为0的结点删除,这里标记为1;
26                 topo[cnt++]  = j+'A';//进拓扑序列;
27                 count++;//找到一个前驱结点,计数器加1;
28 
29                 for(k = j+1; k < n; k++)
30                 {
31                     if(indegree[k] == 0 && !vis[k])
32                         m = 1;//m=1说明有多个前驱结点,拓扑序列不唯一;
33                 }
34                 for(k = 0; k < n; k++)
35                 {
36                     if(map[j][k] == 1)
37                         indegree[k]--;//与j相连的结点入度减一;
38                 }
39                 break;
40             }
41 
42         }
43     }
44     topo[cnt] = '\0';
45     if(count < n)
46         return -1;//若n次循环后前驱结点数小于n说明有环;
47     else if(m == 1) return 0;//m=1说明有多个前驱结点,拓扑序列不唯一;
48     else return 1;
49 }
50 int main()
51 {
52     int i,res;
53     char u,v;
54     while(~scanf("%d %d",&n,&m))
55     {
56         if(n == 0 && m == 0)
57           break;
58         getchar();
59         memset(map,0,sizeof(map));
60         memset(degree,0,sizeof(degree));
61 
62         int ok = 0;
63         for(i = 1; i <= m; i++)
64         {
65             if(ok == 0)
66             {
67                 scanf("%c<%c",&u,&v);
68                 getchar();
69                 degree[v-'A']++;
70                 map[u-'A'][v-'A'] = 1;
71                 res = toposort();
72                 if(res == -1)
73                 {
74                     printf("Inconsistency found after %d relations.\n",i);
75                     ok = 1;
76                     continue;
77                 }
78                 else if(res == 1)
79                 {
80                     printf("Sorted sequence determined after %d relations: ",i);
81                     printf("%s.\n",topo);
82                     ok = 1;
83                     continue;
84                 }
85             }
86             else
87             {
88                 scanf("%c<%c",&u,&v);
89                 getchar();
90             }
91         }
92         if(ok == 0)
93             printf("Sorted sequence cannot be determined.\n");
94     }
95     return 0;
96 }
View Code

 

posted on 2013-08-09 17:02  straw_berry  阅读(150)  评论(0编辑  收藏  举报