
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100 + 15;
int n,m,t;//图中有n个节点
int topo[maxn];//储存拓扑排序的顺序
int staus[maxn];//表示转态
bool Grape[maxn][maxn];//表示依赖关系
bool dfs(int u)
{
staus[u] = -1;//表示正在进行深搜
for(int v=1;v<=n;++v)
{
if(Grape[u][v])
{
if(staus[v]<0)
return false;//说明存在环
if(!staus[v]&&!dfs(v))//如果未被访问过
return false;
}
}
staus[u] = 1;//表示已经深搜完并返回的状态
topo[--t] = u;
return true;
}
bool topoSort()
{
t = n;
memset(staus,0,sizeof(staus));//节点都未被访问过
for(int u=1;u<=n;++u)//深搜每一个节点
{
if(!staus[u])//当未访问过
if(!dfs(u))
return false;
}
return true;
}
int main()
{
while(cin>>n>>m)
{
memset(Grape,false,sizeof(Grape));
if(n==0&&m==0)
break;
int u,v;
for(int i=0;i!=m;++i)
{
cin>>u>>v;
Grape[u][v] = true;
}
topoSort();
cout<<topo[0];
for(int i=1;i!=n;++i)
cout<<" "<<topo[i];
cout<<endl;
}
}
UVA 10305
_______________________________________2019 9 14未完
