hdu_1285 确定比赛名次 (拓扑排序)

来源:互联网 发布:python 加载c 静态库 编辑:程序博客网 时间:2024/05/18 09:55

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 504int  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;}

原创粉丝点击