#include <vector>
#include <iostream>
using namespace std;
int n,m;
vector<vector<int> > a;
vector<int> vis,ans;
void dfs(int i){
if(vis[i]) return;
vis[i]=1;
for(int j=0;j<a[i].size();j++){
dfs(a[i][j]);
}
ans.push_back(i);
}
int main(){
while(cin>>n>>m){
if(n==0&&m==0){
return 0;
}
a.assign(n+1,vector<int>());
vis.assign(n+1,0);
ans.clear();
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
a[x].push_back(y);
}
for(int i=1;i<=n;i++){
dfs(i);
}
for(int i=n-1;i>0;i--){
cout<<ans[i]<<' ';
}
cout<<ans[0]<<endl;
}
}
#include <iostream>
using namespace std;
int n,m;
vector<vector<int> > a;
vector<int> vis,ans;
void dfs(int i){
if(vis[i]) return;
vis[i]=1;
for(int j=0;j<a[i].size();j++){
dfs(a[i][j]);
}
ans.push_back(i);
}
int main(){
while(cin>>n>>m){
if(n==0&&m==0){
return 0;
}
a.assign(n+1,vector<int>());
vis.assign(n+1,0);
ans.clear();
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
a[x].push_back(y);
}
for(int i=1;i<=n;i++){
dfs(i);
}
for(int i=n-1;i>0;i--){
cout<<ans[i]<<' ';
}
cout<<ans[0]<<endl;
}
}
Comments
Post a Comment