# HNOI2015 菜肴制作

### 题目链接:戳我

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAXN 100010
using namespace std;
int n,m,t,T,dfn_clock;
struct Node
{
int id,dfn;
friend bool operator < (Node x,Node y)
{return x.dfn<y.dfn;}
};
struct Edge{int nxt,to;}edge[MAXN<<1];
{
}
inline bool cmp(int x,int y){return ans[x]<ans[y];}
inline void solve()
{
priority_queue<Node>q;
for(int i=1;i<=n;i++)
if(ru[i]==0)
q.push((Node){i,i});
while(!q.empty())
{
int u=q.top().id;q.pop();
ans[u]=dfn_clock,dfn_clock--;
{
int v=edge[i].to;
if(ans[v]) continue;
ru[v]--;
if(ru[v]==0) q.push((Node){v,v});
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
memset(ru,0,sizeof(ru));
memset(ans,0,sizeof(ans));
t=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ru[x]++;
}
dfn_clock=n;
solve();
bool flag=true;
for(int i=1;i<=n;i++)
if(ans[i]==0)
{
printf("Impossible!\n");
flag=false;
break;
}
if(flag==true)
{
for(int i=1;i<=n;i++) p[i]=i;
sort(&p[1],&p[n+1],cmp);
for(int i=1;i<=n;i++) printf("%d ",p[i]); puts("");
}
}
return 0;
}

posted @ 2019-06-19 17:50  风浔凌  阅读(96)  评论(0编辑  收藏  举报