Last active
April 14, 2019 08:56
-
-
Save Chillee/438ec1c687734727660e541c588b4737 to your computer and use it in GitHub Desktop.
Topological Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool vis[MAXN]; | |
void topodfs(int cur, vector<int> &ans) { | |
if (vis[cur]) | |
return; | |
vis[cur] = true; | |
for (auto i : adj[cur]) | |
topodfs(i, ans); | |
ans.push_back(cur); | |
} | |
vector<int> toposort() { | |
vector<int> ans; | |
for (int i = 0; i < N; i++) | |
topodfs(i, ans); | |
reverse(ans.begin(), ans.end()); | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment