Answer the question
In order to leave comments, you need to log in
How to complement 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; ++i)
used[i] = false;
ans.clear();
for (int i=0; i<n+1; ++i)
if (!used[i])
dfs(i);
}
Answer the question
In order to leave comments, you need to log in
With two-digit visit marks (bool used[]) - no way.
You need to switch to three-digit labels (UNVISITED, PARTLY_VISITED, VISITED).
void dfs (int v) {
state[v] = PARTLY_VISITED;
.....
state[v] = VISITED;
ans.push_back (v);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question