Skip to content

Instantly share code, notes, and snippets.

@nafis00141
Created October 10, 2016 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafis00141/c84305ae7fa54f5e695600c880736ece to your computer and use it in GitHub Desktop.
Save nafis00141/c84305ae7fa54f5e695600c880736ece to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
vector<int>v[105];
vector<int>t;
bool visited[105];
void topsort(int x){
visited[x]=true;
for(int i=0;i<v[x].size();i++){
int xx = v[x][i];
if(visited[xx]==false) topsort(xx);
}
t.push_back(x);
}
int main(){
int n,e;
while(cin>>n>>e , !(n==0 && e==0)){
memset(visited,false,sizeof(visited));
t.clear();
for(int i=0;i<100;i++)
v[i].clear();
for(int i=0;i<e;i++){
int a,b;
cin>>a>>b;
v[a].push_back(b);
}
for(int i=1;i<=n;i++){
if(visited[i]==false) topsort(i);
}
int a = t.size();
for(int i=a-1;i>=0;i--){
cout<<t[i];
if(i!=0) cout<<" ";
}
cout<<"\n";
}
return 0;
}
/*
5 4
1 2
2 3
1 3
1 5
0 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment