#include <cstdio>
#include <queue>
#include <vector>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N=30010;
int n,m,e;
int v[N],ans[N];
vector<int> p[N];
void topSort()
{
priority_queue<int> Q;
int i,size,tmp;
for(i=1;i<=n;i++)
if( v[i]==0 ) Q.push(i);
while( !Q.empty() ){
tmp=Q.top(); Q.pop();
size=p[tmp].size();
for(i=0;i<size;i++){
v[ p[tmp][i] ]--;
if( v[p[tmp][i]]==0 ) Q.push( p[tmp][i] );
}
ans[e++]=tmp;
}
}
int main()
{
int i,t,x,y;
scanf("%d",&t);
while( t-- )
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
p[i].clear();
memset(ans,0,sizeof(ans));
for(i=0;i<m;i++){
scanf("%d%d",&x,&y);
v[x]++;
p[y].push_back(x);
}
e=0;
topSort();
for(i=n-1;i>0;i--)
printf("%d ",ans[i]);
printf("%d\n",ans[i]);
}
return 0;
}
hdu 4857
最新推荐文章于 2019-09-05 16:41:27 发布