HDU1285 拓扑排序 xingxing在努力

这题很明显的拓扑排序吧。。 代码如下:注意重边

 

  

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;
const int maxn = 500 + 10;
int N, M;
int Map[maxn][maxn];
int indegree[maxn];

void topo()
{
    priority_queue<int, vector<int>, greater<int> > que;
    for(int i=1; i<=N; i++)
        if(indegree[i] == 0) que.push(i);
    int c = 1;
    while(!que.empty())
    {
        int v = que.top(); que.pop();
        printf("%d%c", v, c==N?'\n':' ');
        c++;
        for(int i=1; i<=N; i++)
        {
            if(Map[v][i])
            {
                indegree[i]--;
                if(indegree[i]==0) que.push(i);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d", &N, &M) == 2)
    {
        memset(indegree, 0, sizeof(indegree));
        memset(Map, 0, sizeof(Map));
        for(int i=1; i<=M; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            if(Map[u][v]==0)
            {
                Map[u][v] = 1;
                indegree[v]++;
            }
        }
        topo();
    }
    return 0;
}

 

posted @ 2015-11-15 23:08  xing-xing  阅读(127)  评论(0)    收藏  举报