# Hdu4857逃生（逆向）

﻿﻿
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 <cstdio> #include <iostream> #include <cstring> #include <string> #include <cmath> #include <queue> #include <set> #include <vector> #include <stack> #include <algorithm> #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define mem(a) memset(a, 0, sizeof(a)) #define eps 1e-5 #define M 100005 #define INF 0x3f3f3f3f using namespace std; vector<int>a[30003]; int t,b,c,aa,bb; int pre[M]; int vis[M]; int main() {   scanf("%d",&t);   while(t--)   {       priority_queue<int>q;       mem(pre);       mem(vis);       scanf("%d%d",&b,&c);       for(int i=1;i<=b;i++)       {           a[i].clear();       }       for(int i=0;i<c;i++)       {         scanf("%d%d",&aa,&bb);           pre[aa]++;           a[bb].push_back(aa);       }       for(int i=1;i<=b;i++)       {           if(pre[i]==0)           {               q.push(i);           }
}       vector<int>res;       int sum=0;       while(!q.empty())       {           sum++;           int e=q.top();           res.push_back(e);           q.pop();           vis[e]=1;           for(int i=0;i<a[e].size();i++)           {               aa=a[e][i];               pre[aa]--;               if(pre[aa]==0)               {                   q.push(aa);               }           }       }       for(int i=res.size()-1;i>=0;i--)       {
if(i==res.size()-1) printf("%d",res[i]);       else printf(" %d",res[i]);            }     puts("");   }   return 0; }