"POJ 1094" sorting It all Out
Topological sorting
Outputs the position and order of the first successful sort (smooth topology and no siblings in the topology)
Or
The first appears in a position that is inconsistent with the previously given conditions (gaps in the topology process)
Pit point is the appearance of any kind of situation on the out
The code is as follows
#include <iostream>using namespace STD;intin[ -],inn[ -],n;intmp[ -][ -];intTopo (intOP) {intI,j,k,f =1; for(i =0; I < n; ++i) Inn[i] = In[i]; for(i =0; I < n; ++i) {k =-1; for(j =0; J < N; ++J) {if(!inn[j]) {if(k! =-1) F =0; K = J; }} j = k;if(j = =-1)return-1;if(OP)Putchar(' A '+J); inn[j]--; for(k =0; K < n; ++K) inn[k]-= mp[j][k]; }if(OP)puts(".");returnF;}intMain () {//Freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout); intF,m,i,a,b,x;Charst[5]; while(~scanf("%d%d", &n,&m) && n && m) {memset(MP,0,sizeof(MP));memset(In,0,sizeof(in)); f =0; x =-1; for(i =1; I <= m; ++i) {scanf('%s ', ST); A = st[0]-' A '; b = st[2]-' A ';if(st[1] ==' > ') {A + = B; b = a A; A-= b; }if(f)Continue; mp[a][b]++; in[b]++; f = Topo (0); x = i; }if(!f)puts("Sorted sequence cannot be determined.");Else if(f = =-1)printf("inconsistency found after%d relations.\n", x);Else{printf("Sorted sequence determined after%d relations:", x); Topo (1); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"POJ 1094" sorting It all Out