HDU1285

拓扑排序模版题



#include <iostream>

#include <stdio.h>
#include <string.h>
#define maxn 500 + 10


using namespace std;


int len[maxn];
int graph[maxn][maxn];
int into[maxn];//入度;




int main()
{
    int n,i,j,m,x,y,count;
    while(scanf("%d%d",&n, &m)!=EOF &&m&&n)
    {
        count = 0;
        memset(into, 0, sizeof(into));
        memset(graph, 0, sizeof(graph));
        memset(len, 0 ,sizeof(len));


        for( i = 1 ; i<=m; i++)
        {
            scanf("%d%d",&x, &y);
            into[y]++;
            graph[x][len[x]++] = y;
        }


        while(count != n)
       {

           if(count) cout<<" ";
           int min = maxn;

         for( int i = 1; i<=n; i++)
          if(into[i] == 0 && i < min)
                min = i;             //找到入度为零且在众多入度为零中数值最小的数;
            into[min] = maxn;   // 把这点排除在外n<500 所以 当count == n 时,这个点的入度还是大于零,可是此时循环已经跳出

          for( int i = 0; i<len[min]; i++)
             into[graph[min][i]]--;
            printf("%d",min);//因为题中说明当有多个答案时要按照从大到小的唯一顺序输出
            count++;
        }
          printf("\n");
    }
    return 0;
}









你可能感兴趣的:(HDU1285)