Main topic:
The title is to give you an n-node, a forward-and-loop graph, and then, for this loop-free graph, we sort him out in topological order, making the sequence in the topological order output in dictionary order.
Problem Solving Ideas:
Apply Toposort () template directly ...
First talk about the meaning of Toposort ():
Topological sequencing that is, when we complete a project, the project is divided into a lot of sub-projects, we can not do a lot of work every time, we have to follow certain things
The internal link between the services to complete the project, such as you want to complete a project, then you must complete the B project, so that B priority is higher than a. So we have a map of the building.
Code:
1# include<cstdio>2# include<iostream>3# include<fstream>4# include<algorithm>5# include<functional>6# include<cstring>7# include<string>8# include<cstdlib>9# include<iomanip>Ten# include<numeric> One# include<cctype> A# include<cmath> -# include<ctime> -# include<queue> the# include<stack> -# include<list> -# include<Set> -# include<map> + - using namespacestd; + A Const DoublePi=4.0*atan (1.0); at -typedefLong LongLL; -typedef unsignedLong LongULL; - -# define INF999999999 -# define MAX521 in - intEdge[max][max];//adjacency Matrix to intIndex[max]; + intBook[max]; - inttop; the intn,m; * $ Panax Notoginseng voidInit () - { theMemset (Edge,0,sizeof(Edge)); + for(inti =1; I <= n;i++ ) A { theIndex[i] =0; +Book[i] =0; - } $ for(inti =0; I < m;i++ ) $ { - intt1,t2; -scanf"%d%d",&t1,&T2); the if(Edge[t1][t2] = =0 ) - {WuyiEDGE[T1][T2] =1; theindex[t2]++; - } Wu } - } About $ voidToposort () - { - intStep; - while(Top <N) A { + for(inti =1; I <= n;i++ ) the { - if(index[i]==0&&book[i]==0 ) $ { theStep =i; the Break; the } the } -Book[step] =1; in if(top) theprintf" "); theprintf"%d", step); About thetop++; the for(intj =1; J <= n;j++ ) the { + if(edge[step][j]==1 ) - { theindex[j]--;Bayi } the } the - } -printf"\ n"); the } the the intMainvoid) the { - while(SCANF ("%d%d", &n,&m)! =EOF) the { the init (); thetop =0;94 Toposort (); the } the the return 0;98}
HDU 1285 determining the tournament position (basis for topological sorting)