For the topic of topological sorting, we must first know two theorems:
1. If the number of elements in the last topology array is less than N, no topological sequence exists. (Circled)
2. If the number of points with zero inbound traffic is greater than 1, the topological sequence is not unique. (The relationship is uncertain)
There is a default thing in this question. If we get to step K (see K <m), we can uniquely identify a sequence, so we don't have to worry about it.
The idea of this question is clear. The input only has x <Y (only the <symbol, and both X and Y are uppercase letters). The data size is small. However, there is no limit on the number of edges. It is not good to use the chained forward star. We should use the adjacent matrix. Each time we use the adjacent matrix, we should note that we should judge the duplicate edge. When we create a small tree, we will use the initialization edge to be infinite, and then take a small value each time. But there is no edge weight here, so as long as there is an edge, the value is 1. In case of a duplicate edge, you cannot add another degree.
You only need to sort the topology to solve this problem. I made a superfluous mistake, and I used Floyd to judge the circle. This is mainly because of insufficient understanding of topological sorting. When the number of letters is less than N, the number of data elements generated by topological sorting is not affected because the number of non-occurrence letters is zero. Only when circles exist can the number of array elements be smaller than N.
Also, I made a very bad mistake. Because Topology Sorting is required if no group of data is input. I used the array for statistics without changing. Stopped. Declare an array again. copy the data in the inbound array before each Topology Sorting.
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 30;int ind[N],que[N], g[N][N],id[N];int iq,flag, T;void topo(int n){ int i,k,j=0,x; T=0; for(i=1;i<=n;i++) { id[i]=ind[i]; if(id[i]==0) que[j++]=i; } if(j>1) T=1; x=j; for(i=0;i<j;i++) { if(j-x>1) T=1; x=j; int u=que[i]; for(k=1;k<=n;k++) { if(g[u][k]&&u!=k) { id[k]--; if(id[k]==0) que[j++]=k; } } } iq=j;}void init(){ flag=0; memset(ind,0,sizeof(ind)); memset(g,0,sizeof(g));}int main(){ //freopen("test.txt","r",stdin); int n,m,i,j,k,t,a,b; char ch1,ch2,ch; while(scanf("%d%d",&n,&m)!=EOF) { if(!n) break; init(); t=0; for(k=1;k<=m;k++) { do scanf("%c",&ch1); while (!isalpha(ch1)) ; scanf("<%c",&ch2); if(flag) continue; a=ch1-64,b=ch2-64; if(a>n||b>n||a==b) flag=1; if(flag) {printf("Inconsistency found after %d relations.\n",k);continue;} if(g[a][b]) continue; g[a][b]=1; ind[b]++; topo(n); if(iq<n){flag=1;printf("Inconsistency found after %d relations.\n",k);continue;} if(iq==n&&!T){ flag=1; printf("Sorted sequence determined after %d relations: ",k); for(i=0;i<n;i++) printf("%c",que[i]+64); printf(".\n"); continue; } if(k==m&&!flag) printf("Sorted sequence cannot be determined.\n"); } } return 0;}
Poj1094sorting it all out topological sorting