UVA 10305:Ordering Tasks(拓扑排序)

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e3+10;
using namespace std;
int a[maxn][maxn];
int way[maxn];
int n,m;
void toposort()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(way[j]==0)
			{
				way[j]--;
				cout<<j;
				if(i!=n)
					cout<<" ";
				for(int k=1;k<=n;k++)
				{
					if(a[j][k])
						way[k]--;
				}
				break;
			}
		}
	}
	cout<<endl;
}
int main(int argc, char const *argv[])
{
	while(cin>>n>>m&&(n||m))//这个不是&&停止!!!
	{
		ms(a);
		ms(way);
		int x,y;
		for(int i=1;i<=m;i++)
		{
			cin>>x>>y;
			a[x][y]=1;
			way[y]++;
		}
		toposort();
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/10324439.html