It's too simple to write "notes."
Topological ordering of graphs
1 //Note: Most topological sorting problems need to be SPJ, because of the different data structures, the topological ordering has many kinds of output. 2#include <iostream>3#include <cstring>4#include <cstdio>5#include <vector>6#include <cmath>7#include <queue>8 using namespacestd;9 structedge{Ten intV,next; One}a[1000001]; A intn,s,tot=0, head[100001],num[100001]; - voidAddintUintv) { -num[v]++; thea[++tot].v=v; -a[tot].next=Head[u]; -head[u]=tot; - } + voidDfsintu) { - intv; +priority_queue<int,vector<int>,greater<int> >q;//You can use queue and stack here. A for(intI=1; i<=u;i++){ at if(num[i]==0){ -num[i]--; - Q.push (i); - } - } - while(!Q.empty ()) { inv=q.top (); - Q.pop (); toprintf"%d", v); + for(inttmp=head[v];tmp!=-1; tmp=A[tmp].next) { -num[a[tmp].v]--; the if(num[a[tmp].v]==0) Q.push (A[TMP].V); * } $ }Panax Notoginseng } - intMain () { theMemset (Head,255,sizeof(head)); +memset (NUM,0,sizeof(num)); Ascanf"%d",&n); the for(intI=1; i<=n;i++){ +scanf"%d",&s); - while(s) { $ Add (i,s); $scanf"%d",&s); - } - } the DFS (n); - return 0;Wuyi }
Topological ordering of graphs