hdu 1285 确定比赛名次

来源:互联网 发布:mac socket 调试工具 编辑:程序博客网 时间:2024/06/16 18:30

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1285

题目大意:n支队伍,m场比赛,输出m场比赛的结果,用x,y表示x的排名在y之前,一次来确定最终的名次。最终结果可能有很多组,需要按字典序小的输出。(输入的数据保证正确,即不存在环)

分析:简单的拓扑排序,因为肯定数据保证正确,所以不需要判断是否存在环。

ac代码:

#include<cstdio>#include<cstring>#define M 505int G[M][M];int topo[M];int q[M];int n, m;void toposort(){int k = 1, top;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(topo[j] == 0){top = j;break;}}q[k++] = top;topo[top] = -1;for(int j = 1; j <= n; j++){if(G[top][j] == 1)topo[j]--;}}}int main(){while(scanf("%d %d", &n, &m) != EOF){memset(G, 0, sizeof(G));memset(topo, 0, sizeof(topo));memset(q, 0, sizeof(q));int x, y;for(int i = 1; i <= m; i++){scanf("%d %d", &x, &y);if(G[x][y] == 0){G[x][y] = 1;topo[y]++;}}toposort();for(int i = 1; i < n; i++)printf("%d ", q[i]);printf("%d\n", q[n]);}return 0;}

原创粉丝点击