Test instructions: It probably means--there is a family gathered together, now by the people inside the family to speak, generational high person first speech. Now give N, and then give the number of n row number I input means the number of descendants of line I, then these numbers are in the back of I.
Like Example 5
0
4 5 1 0
1 0
5 3 0
3 0
1 No number behind
2 followed by 4 5 1
3 followed by a 1
4 followed by 5 3
5 followed by a 3
A little experience of topological sort
(1) First save the diagram and then store the corresponding vertex degree
(2) Select a point without a precursor (i.e., a point with an entry level of 0) in the diagram and join the queue
(3) Delete the edge of the point at which it is starting (minus 1 of the points pointed to by the dot)
(4) Repeat the above operation until there are no points with an entry level of 0
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace Std;int iq,n;int queue[1000],map[1000][1000],indegree[1000];void toposort () {int i,j,k;iq=0;for ( i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (map[i][j]) indegree[j]++;}} for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (indegree[j]==0) {queue[iq++]=j;break;}} indegree[j]=-1;//this point into the after, the degree of this is recorded as-1, in order to avoid the next time it is added to the queue for (k=1;k<=n;k++) {if (map[j][k]) indegree[k]--;}} int main () {int i,v;scanf ("%d", &n), for (i=1;i<=n;i++) {while (scanf ("%d", &v)!=eof&&v) map[i][v]=1;} Toposort (); for (i=0;i<iq-1;i++) printf ("%d", Queue[i]);p rintf ("%d", Queue[i]);}
The first question of topological sequencing [email protected][email protected]
Go--go
POJ 2367 Genealogical Tree "topological sort"