Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void topology(stack<int>&s,int i,bool vis[],vector<int>adj[])
- {
- vis[i]=true;
- for(int j =0 ;j<adj[i].size();j++)
- {
- if(!vis[adj[i][j]])
- topology(s,adj[i][j],vis,adj);
- }
- s.push(i);
- }
- int* topoSort(int V, vector<int> adj[]) {
- bool vis[V]={false};
- stack<int>s;
- for(int i=0;i<V;i++)
- {
- if(!vis[i]){
- topology(s,i,vis,adj);
- }
- }
- int *a = ((int*)malloc(sizeof(int)*V));
- int j=0;
- while(!s.empty())
- {
- a[j++]=s.top();
- s.pop();
- }
- return a;
- }
Add Comment
Please, Sign In to add comment