http://acm.hdu.edu.cn/showproblem.php?pid=1285
分析: 拓扑排序
代码:
//hdu1285 确定比赛名次
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
using namespace std;
#define N 504
int edge[N][N],indegree[N]; //事先初始化
int c[N];
int n,m;
void PoloOrder()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(edge[i][j]) indegree[j]++;
for(int i=1;i<=n;i++){
int j=1;
while(indegree[j]!=0) j++;
c[i]=j;
indegree[j]--;
for(int k=1;k<=n;k++)
if(edge[j][k]) indegree[k]--;
}
for(int i=1;i<n;i++) printf("%d ",c[i]);
printf("%d\n",c[n]);
}
int main()
{
int a,b;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF){
memset(edge,0,sizeof(edge));
memset(indegree,0,sizeof(indegree));
for(int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
edge[a][b]=1;
}
PoloOrder();
}
return 0;
}