码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1285 确定比赛名次(给一个拓扑有序图要求输出拓扑有序序列)

时间:2015-08-02 01:06:26      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:topology

1.如果有向图无回路,那么该图拓扑有序。

2代码:

#include<cstdio>
#include<cstring>
using namespace std;

int mat[505][505];
int n,m;
int in[505];

void topological_sort()
{
    int cnt=n;
    while(cnt--)
    {
        int i;
        for(i=1; i<=n; i++)
        {
            if(in[i]==0)
                break;
        }
        printf("%d",i);
        in[i]=-1;
        if(cnt)
        {
            printf(" ");
        }
        for(int j=1; j<=n; j++)
        {
            in[j]-=(mat[i][j]);
        }
    }
    printf("\n");
}

int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(mat,0,sizeof(mat));
        memset(in,0,sizeof(in));
        for(int i=0; i<m; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            mat[a][b]++;
            in[b]++;
        }
        topological_sort();
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1285 确定比赛名次(给一个拓扑有序图要求输出拓扑有序序列)

标签:topology

原文地址:http://blog.csdn.net/xky1306102chenhong/article/details/47192435

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!