题面毒人,其实就是叫你反图跑拓扑
#include#include #include #include #include #define R(a,b,c) for(register int a = (b); a <= (c); ++ a)#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)#define Max(a,b) ((a) > (b) ? (a) : (b))#define Min(a,b) ((a) < (b) ? (a) : (b))#define Fill(a,b) memset(a, b, sizeof(a))#define Abs(a) ((a) < 0 ? -(a) : (a))#define Swap(a,b) a^=b^=a^=b#define ll long long//#define ON_DEBUG#ifdef ON_DEBUG#define D_e_Line printf("\n\n----------\n\n")#define D_e(x) cout << #x << " = " << x << endl#define Pause() system("pause")#define FileOpen() freopen("in.txt","r",stdin);#else#define D_e_Line ;#define D_e(x) ;#define Pause() ;#define FileOpen() ;#endifstruct ios{ template ios& operator >> (ATP &x){ x = 0; int f = 1; char c; for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar(); x*= f; return *this; }}io;using namespace std;#include const int N = 100007;struct Edge{ int nxt, pre;}e[N << 1];int head[N], cntEdge;inline void add(int u, int v){ e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;}int in[N];priority_queue q;int ans[N];int main(){ int Tasks; io >> Tasks; int flag = 0; while(Tasks--){ int n, m; io >> n >> m; if(flag){ R(i,0,n){ head[i] = in[i] = 0; } cntEdge = 0; } R(i,1,m){ int u, v; io >> u >> v; add(v, u); ++in[u]; } int tot = 0; R(i,1,n){ if(!in[i]){ q.push(i); } } while(!q.empty()){ int u = q.top(); q.pop(); ans[++tot] = u; for(register int i = head[u]; i; i = e[i].nxt){ int v = e[i].pre; --in[v]; if(!in[v]){ q.push(v); } } } if(tot != n){ printf("Impossible!\n"); flag = 1; continue; } nR(i,n,1){ printf("%d ", ans[i]); } putchar('\n'); flag = 1; } return 0;}