邻接矩阵,我的模板
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int sizen=1000;
bool Map[sizen][sizen];
int degree[sizen];
bool mark[sizen];
void tosort(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(!degree[j]&&!mark[j])
{
mark[j]=true;
if(i==n)
printf("%d\n",j);
else
printf("%d ",j);
for(k=1;k<=n;k++)
if(Map[j][k])
degree[k]--;
break;
}
}
}
int main()
{
int V,E;
int x,y;
while(scanf("%d%d",&V,&E)!=EOF)
{
memset(mark,false,sizeof(mark));
memset(Map,false,sizeof(Map));
memset(degree,0,sizeof(degree));
while(E--)
{
scanf("%d%d",&x,&y);
if(!Map[x][y])
{
Map[x][y]=true;
degree[y]++;
}
}
tosort(V);
}
return 0;
}