-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdfs.cpp
More file actions
31 lines (27 loc) · 747 Bytes
/
Copy pathdfs.cpp
File metadata and controls
31 lines (27 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
bool dfs(int u, vector<vector<int>>& g, vector<int>& vis, vector<int>& order) {
vis[u] = 1;
for (int v : g[u]) {
if (vis[v] == 1) return false; // cycle
if (vis[v] == 0 && !dfs(v, g, vis, order)) return false;
}
vis[u] = 2;
order.push_back(u);
return true;
}
int main() {
int n = 6;
vector<vector<int>> g = {
{1, 2}, {3}, {3}, {4}, {5}, {}
};
vector<int> vis(n, 0), order;
for (int i = 0; i < n; i++)
if (vis[i] == 0 && !dfs(i, g, vis, order)) {
cout << "Cycle detected\n";
return 0;
}
reverse(order.begin(), order.end());
for (int x : order) cout << x << " ";
cout << endl;
}