杭电1285

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int mapp[600][600];
int degree[600];
int visit[600];
int main()
{
	int N,M;
	int i,n,m,num;
	int p[600];
	priority_queue<int, vector<int>, greater<int> > q;
	int sum;
	while(~scanf("%d%d",&N,&M))
	{
		sum=0;
		memset(mapp,0,sizeof(mapp));
		memset(degree,0,sizeof(degree));
		memset(visit,0,sizeof(visit));
		for(i=1;i<=M;i++)
		{
			scanf("%d%d",&n,&m);
			if(mapp[n][m])continue;
			mapp[n][m]=1;
			degree[m]++;
		}
		for(i=1;i<=N;i++)
		{
			if(degree[i]==0)
			{
				q.push(i);
			}
		}
		while(!q.empty())
		{
			num=q.top();
			q.pop();
			p[sum++]=num;
			for(i=1;i<=N;i++)
			{
				if(mapp[num][i])
				{
				degree[i]--;
				if(degree[i]==0)
				q.push(i);
				}
			}
		}
		for(i=0;i<sum-1;i++)
		{
			printf("%d ",p[i]);
		}
		printf("%d\n",p[sum-1]);
	}
return 0;
}

你可能感兴趣的:(杭电1285)