# 【题解】逃生 HDU - 4857 (拓扑排序 反向建图)⭐⭐⭐

## 逃生 HDU - 4857

Sample Input
1
5 10
3 5
1 4
2 5
1 2
3 4
1 4
2 3
1 5
3 5
1 2
Sample Output
1 2 3 4 5

### Hint

#### 经验小结:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 30010;

int N, M;
vector<int> es[maxn], topo;
int in[maxn];
void topoSort(){

topo.clear();
priority_queue<int, vector<int>, less<int> > q;
for(int i = 1; i <= N; i++)
if(in[i]==0)
q.push(i);
while(!q.empty()){
int u = q.top();
q.pop();
topo.push_back(u);
for(int i = 0; i < es[u].size(); i++){
int v = es[u][i];
--in[v];
if(in[v]==0) q.push(v);
}
}
}
int main()
{
int a, b, T;
scanf("%d",&T);
while(T--){
ms(es, 0); ms(in, 0);
scanf("%d%d",&N,&M);
for(int i = 1; i <= M; i++){
scanf("%d%d",&a,&b);
es[b].push_back(a);     //反向建图
++in[a];
}
topoSort();
for(int i = topo.size()-1; i >= 0; i--){
if(i==0) printf("%d",topo[i]);
else printf("%d ",topo[i]);
}
printf("\n");
}

return 0;
}