Skip to content

Instantly share code, notes, and snippets.

@Superty
Last active May 15, 2016 21:55
Show Gist options
  • Save Superty/2ca9fc925840e0a3caf9 to your computer and use it in GitHub Desktop.
Save Superty/2ca9fc925840e0a3caf9 to your computer and use it in GitHub Desktop.
performing topological sort
int deg[maxn], rank[maxn];
void topologicalSort()
{
queue<int> q;
for(int i = 1; i <= n; i++)
{
if(deg[i] == 0)
{
rank[i] = 0;
q.push(i);
}
}
while(!q.empty())
{
int v = q.front());
q.pop();
for(int i = 0; i < e[v].size(); i++)
{
deg[e[v][i]]--;
if(deg[e[v][i]] == 0)
{
rank[e[v][i]] = rank[v] + 1;
q.push(e[v][i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment