# 逃生

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6682    Accepted Submission(s): 1954

Problem Description

Input

Output

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

————————————————————————————————————————————————————————

#include<bits/stdc++.h>
using namespace std;

const int N = 30005;

int n,m;
vector<int>mapp[N];
int in[N];
int order[N];

void TOP_sort(){
int num=0;
priority_queue<int >Q;
for(int i=1;i<=n;i++){
if(in[i]==0){
Q.push(i);
}
}
while(!Q.empty()){
int top = Q.top();
Q.pop();
order[num++] = top;
for(int i=0;i<mapp[top].size();i++){
in[mapp[top][i]]--;
if(in[mapp[top][i]]==0)
Q.push(mapp[top][i]);
}
}
if(num==n){
for(int i=n-1;i>=1;i--){
printf("%d ",order[i]);
}
cout<<order[0]<<endl;
}
}

int main(){
int t;
cin>>t;
while(t--){
memset(in,0,sizeof(in));
memset(order,0,sizeof(order));
for(int i=0;i<=n;i++)   mapp[i].clear();
scanf("%d%d",&n,&m);
int x,y;
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
mapp[y].push_back(x);
in[x]++;//入度+1
}
TOP_sort();
}
return 0;
}
/*
3
5 10
3 5
1 4
2 5
1 2
3 4
1 4
2 3
1 5
3 5
1 2

3 1
3 1

4 3
1 2
2 3
4 3

1 2 3 4 5
3 1 2
1 2 4 3
*/