# 逃生

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

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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
priority_queue<int>q;
vector<int>v[30005];
stack<int>s;
int people,num;
int in[30005];
void init()
{
for(int i=0; i<=30000; i++)
v[i].clear();
while(!q.empty())
{
q.pop();
}
while(!s.empty())
{
s.pop();
}
memset(in,0,sizeof(in));
}
int cnt=0;
int solve()
{
for(int i=1; i<=people; i++)
if(in[i]==0)q.push(i);
while(!q.empty())
{
int now=q.top();
cout<<now<<"=="<<endl;
q.pop();
cnt++;
for(int i=0; i<v[now].size(); i++)
{
in[v[now][i]]--;
if(in[v[now][i]]==0)
q.push(v[now][i]);
}
s.push(now);
}

}
int main()
{
std::ios::sync_with_stdio(false);
int test;
cin>>test;
while(test--)
{
init();

cin>>people>>num;
while(num--)
{
int a,b;
cin>>a>>b;
in[a]++;
v[b].push_back(a);
}
solve();
int cnt=0;
while(!s.empty())
{
int now=s.top();
s.pop();
cnt++;
if(cnt==people)
cout<<now<<endl;
else
cout<<now<<" ";
}
}
}