Sorting It All Out

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29855   Accepted: 10341

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 #include <iostream>
 2 using namespace std;
 3 int map[27][27];
 4 int point_num,edge_num;
 5 int degree[27];
 6 char order[27];
 7 int top_sort(){
 8     int flag=1;
 9     int p=-1;
10     for(int j=0;j<27;j++){
11         degree[j]=0;
12         order[j]='';
13     }
14     for(int i=0;i<point_num;i++){
15         for(int j=0;j<point_num;j++){
16             if(map[i][j])degree[j]++;
17         }
18     }
19 
20     for(int i=0;i<point_num;i++){
21         p=-1;
22         for(int j=0;j<point_num;j++){
23             if(degree[j]==0){
24                 if(p==-1) p=j;
25                 else flag=0;
26             }
27         }
28         if(p==-1) return 0;
29         degree[p]=-1;
30         order[i]=p+'A';
31         for(int j=0;j<point_num;j++)
32          {
33             if(map[p][j]) degree[j]--;
34          }
35     }
36     if(flag) return 1;
37     else return 2;
38 }
39 int main() {
40     cin>>point_num>>edge_num;
41     while(point_num&&edge_num){
42         int flag=0;
43         for(int i=0;i<27;i++){
44             for(int j=0;j<27;j++){
45                 map[i][j]=0;
46             }
47         }
48         for(int i=0;i<edge_num;i++){
49             char a,b,c;
50             cin>>a>>b>>c;
51             if(flag) continue;
52             map[a-'A'][c-'A']=1;
53             int result=top_sort();
54             if(result==0){
55                  cout<<"Inconsistency found after "<<i+1<<" relations.
";
56                  flag=1;
57              }
58             else if(result==1)
59              {
60                  cout<<"Sorted sequence determined after "<<i+1<<" relations: "<<order<<".
";
61                  flag=1;
62              }
63          }
64          if(!flag) cout<<"Sorted sequence cannot be determined.
";
65          cin>>point_num>>edge_num;
66     }
67     return 0;
68 }
原文地址:https://www.cnblogs.com/sdxk/p/4664172.html