POJ1094 Sorting It All Out
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 40012 | Accepted: 14072 |
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、在 拓撲排序時,當出現同時可訪問兩個節點或多個節點時,則資訊不完全。
2、當出現有環時,則有衝突,判斷是否有環可以在拓撲排序完時判斷是否有點未訪問。因為若無環,每個點都可以訪問。
不多說了,附上我的AC程式碼:
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 const int MAXN=2500; 7 int n,m,cas,id; 8 9 struct edge 10 { 11int v,nx; 12 }set[MAXN]; 13 int head[30],d[30],ok[30],write[30]; 14 queue<int> Q; 15 16 void Addedge(int u,int v) 17 { 18id++;set[id].v=v;set[id].nx=head[u]; 19head[u]=id; 20 } 21 22 int bfs() 23 { 24cas=0; 25while(!Q.empty())Q.pop(); 26int out=1,t,u;t=0; 27for(int i=1;i<=n;i++) 28if(!ok[i]) 29{ 30t++;Q.push(i); 31} 32if(t>1)out=0; 33while(!Q.empty()) 34{ 35u=Q.front();Q.pop();t=0;write[++cas]=u; 36for(int k=head[u];k>0;k=set[k].nx) 37{ 38ok[set[k].v]--; 39if(!ok[set[k].v]) 40{ 41t++;Q.push(set[k].v); 42} 43} 44if(t>1)out=0; 45} 46for(int i=1;i<=n;i++)if(ok[i]>0){out=-1;break;} 47return out; 48 } 49 50 char print() 51 { 52for(int i=1;i<=cas;i++)printf("%c",(char)(write[i]+64)); 53return '.'; 54 } 55 56 int main() 57 { 58while(~scanf("%d%d",&n,&m)) 59{ 60memset(write,0,sizeof(write)); 61memset(d,0,sizeof(d)); 62memset(head,-1,sizeof(head)); 63id=0; 64int now=0,loca; 65char a,b,c; 66if(n==0 && m==0)break; 67for(int i=1;i<=m;i++) 68{ 69cin>>a>>b>>c; 70if(b=='<') 71{ 72Addedge(a-64,c-64);d[c-64]++; 73} 74else 75{ 76Addedge(c-64,a-64);d[a-64]++; 77} 78if(now==0) 79{ 80for(int j=1;j<=n;j++)ok[j]=d[j]; 81now=bfs();loca=i; 82} 83} 84if(now==0)puts("Sorted sequence cannot be determined."); 85else if(now==1)printf("Sorted sequence determined after %d relations: ",loca),printf("%c\n",print()); 86else printf("Inconsistency found after %d relations.\n",loca); 87} 88return 0; 89 }
記得點贊歐。