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

    确定比赛名次

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


    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
     
     
    图的拓扑排序,题目要求输出时编号小的队伍在前,可使用优先队列。
     
     1 #include <cstdio>
     2 #include <vector>
     3 #include <functional>
     4 #include <queue>
     5 using namespace std;
     6 bool cmp(const int &x,const int &y)
     7 {
     8     return x>y;
     9 }
    10 
    11 const int maxn=550;
    12 vector<int> v[maxn];
    13 int indegree[maxn];
    14 priority_queue<int,vector<int>,greater<int> >q;
    15 queue<int> s;
    16 int main()
    17 {
    18     int n,m,a,b;
    19     while(scanf("%d%d",&n,&m)==2)
    20     {
    21         for(int i=1;i<=n;i++) indegree[i]=0;
    22         for(int i=0;i<m;i++)
    23         {
    24             scanf("%d%d",&a,&b);
    25             v[a].push_back(b);
    26             indegree[b]++;
    27         }
    28         for(int i=1;i<=n;i++)
    29             if(indegree[i]==0) q.push(i);
    30         while(!q.empty())
    31         {
    32             int t=q.top(); q.pop();
    33             for(int i=0;i<v[t].size();i++)
    34             {
    35                 indegree[v[t][i]]--;
    36                 if(indegree[v[t][i]]==0) q.push(v[t][i]);
    37             }
    38             s.push(t);
    39         }
    40         int first=true;
    41         while(!s.empty())
    42         {
    43             if(first){printf("%d",s.front()); first=false;}
    44             else  printf(" %d",s.front());
    45             s.pop();
    46         }
    47         printf("
    ");
    48         for(int i=1;i<=n;i++) v[i].clear();
    49     }
    50     
    51 }
  • 相关阅读:
    python简介
    Error:unknown filesystem
    Ubuntu 12.04 相关问题
    C/C++版数据结构之链表<一>
    C/C++版数据结构之链表<二>
    C/C++版数据结构之树<二>
    C/C++版数据结构之树<一>
    C/C++版数据结构之链表<三>
    C/C++版数据结构之排序算法
    php 操作文件简单例子
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5185070.html
Copyright © 2011-2022 走看看