hdu 1285 确定比赛名次

简介:

click here ~~

                                               确定比赛名次
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17531    Accepted Submission(s): 7005



Problem DescriptionN个比赛队(1<=N<=500),编号依次为123,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1P2,用P1P2表示,排名时P1P2之前。现在请你编程序确定排名。





Input

输入有若干组,每组中的第一行为二个数N1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1P2表示即P1队赢了P2队。





Output

给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

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





Sample Input

4 3
1 2
2 3
4 



3





Sample Output

1 2 4 3

AI 代码解读

解题思路:拓扑排序 + 优先队列
代码:

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 1505;
//从小到大
struct cmp
{
    bool operator () (int x, int y)
    {
        return x > y;
    }
};
int degree[maxn];
int n, m, seg[maxn];
vector<int>vec[maxn];
//拓扑排序
int topo()
{
    priority_queue<int,vector<int>,cmp>q;
    int in[maxn];
    for(int i=1; i<=n; i++)
    {
        in[i] = degree[i];
        if(!in[i])
            q.push(i);
    }
    int k = 0;
    bool ret = false;
    while(!q.empty())
    {
        if(q.size() != 1)
            ret = true;
        int u = q.top();
        q.pop();
        seg[k++] = u;
        for(int i=0; i<vec[u].size(); i++)
        {
            int v = vec[u][i];
            in[v]--;
            if(!in[v])
                q.push(v);
        }
    }
    if(k < n)
        return -1;
    if(ret)
        return 0;
    return 1;
}
int main()
{
    while(cin>>n>>m)
    {
        for(int i=0; i<=n; i++)
        {
            vec[i].clear();
            degree[i] = 0;
        }
        for(int i=0; i<m; i++)
        {
            int u, v;
            cin>>u>>v;
            vec[u].push_back(v);
            degree[v]++;
        }
        for(int i=1; i<=n; i++)
            sort(vec[i].begin(), vec[i].end());
        topo();
        for(int i=0; i<n; i++)
        {
            if(i == n-1)
                printf("%d\n",seg[i]);
            else
                printf("%d ",seg[i]);
        }
    }
    return 0;
}
AI 代码解读
目录
打赏
0
0
0
0
2
分享
相关文章
openeuler系统远程访问配置
openeuler系统远程访问配置
319 0
openeuler系统远程访问配置
spinal HDL - 11 - 使用状态机语法编写“1001“序列检测
spinal HDL - 11 - 使用状态机语法编写“1001“序列检测
487 0
spinal HDL - 11 - 使用状态机语法编写“1001“序列检测
全面了解三维重建在建筑领域应用:多种技术思路、落地案例全都有
全面了解三维重建在建筑领域应用:多种技术思路、落地案例全都有
566 0
云数据库RDS是什么?
您还在使用自建数据库吗?要自建机房、采购设备、安装软件,还要不断投入运维管理,既费时又耗钱。赶快放弃传统的开发模式,来使用阿里云的云数据库RDS吧。 云数据库RDS具有低成本、高性能、轻量运维,且即买即用,随意变配的特点。
3357 0
YOLO虚幻合成数据生成器
UnrealSynth 基于 UE5 虚幻引擎开发,目前支持 YOLO 系列模型合成数据的生成。
263 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等