1 int c[N]; 2 int topo[N], t; 3 bool dfs(int u) 4 { 5 c[u] = -1; 6 for(int i = head[u]; ~i; i = e[i].to) 7 { 8 if(c[i] < 0) return false; 9 else if(!c[i] && !dfs(i)) return false; 10 } 11 c[u] = 1; topo[--t] = u; 12 return true;
13 } 14 bool toposort() 15 { 16 t = n; 17 memset(c, 0, sizeof(c)); 18 for(int i = 0; i < u; i++) 19 if(!c[i]) 20 if(!dfs(i)) 21 return false; 22 return true; 23 }