nyoj349 Sorting It All Out (拓扑排序)
Sorting It All Out
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
-
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 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.
- 输出
- 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. - 样例输入
-
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
- 样例输出
-
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
-
算法分析:通过判断入度来的情况;
-
定义一个变量m来判断入度为0的个数;如果m==0自然是有环的直接return ;
-
如果m>1标记一下;找到一个入度为0节点,存入ans中同时他的入度标为-1,在把他有关系的节点自减;
-
循环以上操作n次;
-
View Code
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #define N 30
5
6 using namespace std;
7
8 int map[N][N];
9 int in[N],ans[N];
10 bool flag1,flag2;
11
12 void work(int n)
13 {
14 int k=0,i,j,temp[N],m,loc;
15 bool flag=false;
16 memset(temp,0,sizeof(temp));
17 for(i=0;i<n;i++) temp[i]=in[i];
18 for(i=0;i<n;i++)
19 {
20 m=0;
21 for(j=0;j<n;j++)
22 if(temp[j]==0)
23 {
24 m++;loc=j;
25 }
26 if(m==0)
27 {
28 flag2=true;
29 return ;
30 }
31 if(m>1) flag=true;
32 ans[k++]=loc;
33 temp[loc]=-1;
34 for(j=0;j<n;j++)
35 if(map[loc][j]==1)
36 temp[j]--;
37 }
38 if(!flag) flag1=true;
39 }
40
41 int main()
42 {
43 int n,m;
44 while(cin>>n>>m&&(n||m))
45 {
46 flag1=flag2=false;
47 memset(map,0,sizeof(map));
48 memset(in,0,sizeof(in));
49 int i,p;
50 char u,v,t;
51 for(i=1;i<=m;i++)
52 {
53 cin>>u>>t>>v;
54 if(!flag1&&!flag2)
55 {
56 map[u-'A'][v-'A']=1;
57 in[v-'A']++;
58 work(n);
59 p=i;
60 }
61 }
62 if(flag1)
63 {
64 cout<<"Sorted sequence determined after "<<p<<" relations: ";
65 for(i=0;i<n;i++)putchar(ans[i]+'A');
66 cout<<"."<<endl;
67 continue;
68 }
69 if(flag2)
70 {
71 cout<<"Inconsistency found after "<<p<<" relations."<<endl;
72 continue;
73 }
74 cout<<"Sorted sequence cannot be determined."<<endl;
75 }
76 return 0;
77 }