hdu 1285(拓扑排序)

http://acm.split.hdu.edu.cn/showproblem.php?pid=1285

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 510;
typedef long long LL;
int a[maxn], b[maxn], G[maxn][maxn];
int n, m;

void top_sort()
{
    for(int i=1; i<=n; i++)
    {
        int k = 1;
        while(a[k]) k++;///找到一名没有输过一场的成员
        b[i]=k;
        a[k]--;

       for(int j=1; j<=n; j++)
       {
           if(G[k][j])///和K比过的输的成员的次数减1
            a[j]--;
       }
    }
}

int main()
{
    int u, v;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        memset(G, 0, sizeof(G));
        memset(a, 0, sizeof(a));

        for(int i=1; i<=m; i++)
        {
            scanf("%d %d", &u, &v);
            G[u][v] = 1;
        }

        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(G[i][j])
                    a[j]++;///统计每个成员输的次数
            }
        }

        top_sort();

        for(int i=1; i<=n; i++)
            printf("%d%c", b[i], (i==n?'
':' '));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/daydayupacm/p/5778882.html