Skip to content
Snippets Groups Projects
Commit 8e93f6cd authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Fix topological sort

parent a3590b78
Branches
No related tags found
No related merge requests found
......@@ -8,29 +8,29 @@ vector<int> graph[MAX];
struct TopologicalSort {
int N;
stack<int> S;
vector<int> cont;
vector<int> vis;
Topological(int N) :
N(N), cont(N)
TopologicalSort(int N) :
N(N), vis(N)
{}
void init() {
fill(all(cont), 0);
fill(all(vis), 0);
}
/// Fills stack and check for cycles.
/// @param x any vertex
bool dfs(int x) {
cont[x] = 1;
vis[x] = 1;
for (auto i : graph[x]) {
if (cont[i] == 1)
if (vis[i] == 1)
return true;
if (!cont[i] && dfs(i))
if (!vis[i] && dfs(i))
return true;
}
cont[x] = 2;
vis[x] = 2;
S.push(x);
return false;
......@@ -43,7 +43,7 @@ struct TopologicalSort {
bool cycle = false;
for (int i = 0; i < N; ++i) {
if (!cont[i])
if (!vis[i])
cycle |= dfs(i);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment