## playboy307

Java，golang后台开发，欢迎互相交流学习

# 逃生

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

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

Author
CLJ

Source

Recommend
We have carefully selected several similar problems for you:  5566 5565 5564 5563 5562

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 301000
struct node
{
int u,v;
int next;
}edge[MAX];
int head[MAX];
int dre[MAX],cnt,d[MAX],ans,m,n;
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void dijkstra()
{
priority_queue<int>q;
for(int i=1;i<=m;i++)
if(dre[i]==0)
q.push(i);
while(!q.empty())
{
int u=q.top();
q.pop();
d[ans++]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
dre[v]--;
if(dre[v]==0)
q.push(v);
}
}
for(int i=ans-1;i>=1;i--)
printf("%d ",d[i]);
printf("%d\n",d[0]);
}
int main()
{
int a,b;
int t;
scanf("%d",&t);
while(t--)
{
cnt=ans=0;
memset(dre,0,sizeof(dre));
memset(head,-1,sizeof(head));
scanf("%d%d",&m,&n);
while(n--)
{
scanf("%d%d",&a,&b);
add(b,a);
dre[a]++;
}
dijkstra();
}
return 0;
}

07-23 1263

12-25 396

07-21 154

08-19 260

08-05 296

05-07 10

05-03 788

11-07 166

07-08 208