How to add topological sorting?

Part of the code, DFS and topological sort:
void dfs (int v) {
 used[v] = true;
 for (size_t i=0; i<g[v].size(); ++i) { int to= - "g[v][i];" if (!used[to]) dfs (to);} ans.push_back (v); } void t_sort() for (int i="0;" i<n; used[i]="false;" ans.clear(); i<n+1; (!used[i]) dfs(i); }< code></g[v].size();>
<br / > Can't make the sorting occurred in the presence of a cycle in the graph.
September 18th 19 at 23:55
1 answer
September 18th 19 at 23:57
Solution
With double-digit marks attendance (bool used []) all.

We must move on to three-digit labels (UNVISITED, PARTLY_VISITED, VISITED).

void dfs (int v) {
 state[v] = PARTLY_VISITED;
.....
 state[v] = VISITED;
 ans.push_back (v);
}


A good example is in your previous question.

Find more questions by tags GraphsAlgorithmsProgrammingC++