Skip to content

Instantly share code, notes, and snippets.

@lsiddiqsunny
Last active May 6, 2018 16:54
Show Gist options
  • Save lsiddiqsunny/c4af89902d216bdbd1d4e8aa622765e1 to your computer and use it in GitHub Desktop.
Save lsiddiqsunny/c4af89902d216bdbd1d4e8aa622765e1 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
#define mx 200005
vector<int>g[mx];
int visited[mx];
stack<int>s;
void dfs(int u)
{
visited[u]=1;
for(int i=0; i<g[u].size(); i++)
{
int v=g[u][i];
if(visited[v]==0) dfs(v);
}
s.push(u);
}
int main()
{
int n;
cin>>n; //number of vertex
int m;
cin>>m; //number of edges
while(m--)
{
int u,v;
cin>>u>>v;
g[u].push_back(v); //making one directional graph
}
for(int i=1;i<=n;i++){
if(visited[i]==0) dfs(i);
}
for(int i=0; i<n; i++)
{
int x=s.top();
s.pop();
cout<<x<" ";
}
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment