HDU 1285 确定比赛名次(简单拓扑排序)

时间:2023-03-09 22:42:35
HDU 1285 确定比赛名次(简单拓扑排序)

题目链接: 传送门

确定比赛名次

Time Limit: 1000MS     Memory Limit: 65536K

Description

有N个比赛队(1

Input

输入有若干组,每组中的第一行为二个数N(1

Output

给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。

Sample Input

4 3
1 2
2 3
4 3

Sample Output

1 2 4 3
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    int N,M;
    while (~scanf("%d%d",&N,&M))
    {
        int Indegree[505] = {0};
        vector<int>Map[505],Vex;
        priority_queue<int,vector<int>,greater<int> >que;
        int p1,p2;
        while (M--)
        {
            scanf("%d%d",&p1,&p2);
            Map[p1].push_back(p2);
            Indegree[p2]++;
        }

        for (int i = 1;i <= N;i++)
        {
            if (Indegree[i] == 0)
            {
                que.push(i);
            }
        }

        while (!que.empty())
        {
            int val = que.top();
            que.pop();
            Vex.push_back(val);
            for (int i = 0;i < Map[val].size();i++)
            {
                if (--Indegree[Map[val][i]] == 0)
                {
                    que.push(Map[val][i]);
                }
            }
        }
        bool first = true;
        for (int i = 0;i < Vex.size();i++)
        {
            first?printf("%d",Vex[i]):printf(" %d",Vex[i]);
            first = false;
        }
        printf("\n");
    }
    return 0;
}