#include #include #include #include using namespace std; const int MX = 100007; // maximum number of nodes in the graph. int n,m,u,v; vector G[MX]; // adjacency list. int in[MX]; vector TS; // the output vector with nodes sorted in topological order. int main() { /* Input format: n - number of nodes m - number of edges we assume nodes are enumbered 1..n next m lines contains edges descriptions represented as a pair of integers. */ scanf("%d %d", &n, &m); while(m--) { scanf("%d %d", &u, &v); G[u].push_back(v); // add edge to adjcency list in[v]++; // increase input order of v } // Add all the nodes with zero input order to the awswer. for(int i=1; i<=n; ++i) if(in[i]==0) TS.push_back(i); // For every noded that we added to the Topological Order so far: for(int i=0; ineighbour). for(int j = 0; j