//#include <bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<time.h>
#include <iomanip>
#define lowbit(x) (x&(-x))
#define inf 0x7fffffff
#define linf 0x7fffffffffffffff
#define mem(x,y) memset(x,y,sizeof(x))
#define fup(i,x,y) for(int i=(x);i<=(y);i++)
#define fdn(i,x,y) for(int i=(x);i>=(y);i--)
#define sp(x) setprecision(x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define sc(n) scanf("%s",n)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define pff(x) printf("%lf\n",x)
#define debug printf("!!\n");
#define N 1005
#define M 4000009
#define pi acos(-1)
#define eps 1e-2
//cout.setf(ios::fixed);
//freopen("out.txt","w",stdout);// freopen("in.txt","r",stdin);
using namespace std;
typedef long long ll;
typedef double db;
const int mod=1e9+7;
struct edge
{
int d,next;
edge():next(-1){}
}e[N];
int head[N],which;
void add(int x,int y)
{
e[++which].d=y;
e[which].next=head[x];
head[x]=which;
}
int vis[N],a[N],cnt;
int dfs(int x)
{
vis[x]=-1;
for(int i=head[x];i!=-1;i=e[i].next)
{
int tp=e[i].d;
if(vis[tp]==-1) return 0;
else if(!vis[tp]&&!dfs(tp)) return 0;
}
vis[x]=1;
a[cnt--]=x;
return 1;
}
void solve()
{
int n,m;
while(~sdd(n,m)&&n+m)
{
cnt=n;
which=0;
mem(head,-1);
mem(vis,0);
fup(i,1,m)
{
int x,y;
sdd(x,y);
add(x,y);
}
int f=0;
fup(i,1,n)
{
if(!vis[i])
{
if(!dfs(i))
{
f=1;
break;
}
}
}
if(!f)
{
fup(i,1,n)
printf("%d%c",a[i],i!=n?' ':'\n');
}
}
}
int main()
{
int tt=1,k=1;
// sd(tt);
// getchar();
while(tt--)
{
solve();
}
return 0;
}
UVA - 10305 拓扑排序
最新推荐文章于 2019-07-15 17:19:20 发布