zoukankan      html  css  js  c++  java
  • HDU 1258 确定比赛名次

    确定比赛名次

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8837    Accepted Submission(s): 3439


    Problem Description
    有 N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定 排名。
     
    Input
    输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。
     
    Output
    给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

    其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
     
    Sample Input
    4 3 1 2 2 3 4 3
     
    Sample Output
    1 2 4 3
     
    Author
    SmallBeer(CML)
     
    Source
     
    Recommend
    lcy
     
    思路:
    优先queue + 拓扑排序
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <cstring>
    using namespace std;
    int n,m;
    int x,y;
    int queuee[550];
    int head[550];
    int indegree[550];
    struct EdgeNode
    {
        int to;
        int next;
    };
    struct Node
    {
        int x;
        friend bool operator < (const Node & a,const Node & b)
        {
              return a.x > b.x;
        }
    };
    EdgeNode edge[550];
    int hash[550][550];
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(head,0,sizeof(head));
            memset(queuee,0,sizeof(queuee));
            memset(edge,0,sizeof(edge));
            memset(hash,0,sizeof(hash));
            memset(indegree,0,sizeof(indegree));
            for(int k = 1;k <= m;k ++)
            {
                scanf("%d%d",&x,&y);
                if(hash[x][y] == 0)
                {
                   edge[k].to = y;
                   edge[k].next = head[x];
                   head[x] = k;
                   indegree[y] ++;
                   hash[x][y] = 1;
                }
            }
            int iq = 0;
            priority_queue < Node > q;
            for(int i = 1;i <= n;i ++)
            {
                if(indegree[i] == 0)
                {
                   Node p;p.x = i;
                   q.push(p);
                }
            }
            while(!q.empty())
            {
                Node i = q.top();q.pop();queuee[iq ++] = i.x;
                for(int k = head[i.x];k != 0;k = edge[k].next)
                {
                    indegree[edge[k].to] --;
                    if(indegree[edge[k].to] == 0)
                    {
                        Node d;d.x = edge[k].to;
                        q.push(d);
                    }
                }
            }
            for(int i = 0;i < iq - 1;i ++)
                printf("%d ",queuee[i]);
            printf("%d",queuee[iq - 1]);
            printf(" ");
        }
    }

  • 相关阅读:
    django-debug-toolbar的配置以及使用
    Django+telnetlib实现webtelnet
    Centos7 安装 telnet 服务
    Django+paramiko实现webshell
    Zabbix 历史数据存储到 Elasticsearch
    CA和自签证书
    linux加密算法与应用
    LVM拆除pv对应的磁盘
    目录迁移
    添加一个swap设备
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3317693.html
Copyright © 2011-2022 走看看