Skip to content

Instantly share code, notes, and snippets.

@Chillee
Last active April 14, 2019 08:56
Show Gist options
  • Save Chillee/438ec1c687734727660e541c588b4737 to your computer and use it in GitHub Desktop.
Save Chillee/438ec1c687734727660e541c588b4737 to your computer and use it in GitHub Desktop.
Topological Sort
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