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

    确定比赛名次

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


    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
     
     
     1 //2018年5月27日 11:26:43
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <queue>
     6 using namespace std;
     7 
     8 const int N = 500;
     9 const int M = 1001;
    10 
    11 int n, m;
    12 int inDeg[N];
    13 
    14 int fir[N], cnt;
    15 struct Edge{
    16     int nxt;
    17     int to;
    18 }E[M];
    19 
    20 void addEdge(int x, int y){
    21     E[++cnt].to = y;
    22     E[cnt].nxt = fir[x];
    23     fir[x] = cnt;
    24 }
    25 
    26 void Topsort(){
    27     priority_queue<int, vector<int>, greater<int> > q;
    28     for(int i=1; i<=n; i++)
    29         if(inDeg[i] == 0)
    30             q.push(i);
    31     int flag = 0;
    32     while(!q.empty()){
    33         int v = q.top();
    34         q.pop();
    35         if(flag) printf(" ");
    36         printf("%d", v);
    37         flag = 1;
    38         for(int i=fir[v]; i; i=E[i].nxt){
    39             inDeg[E[i].to]--;
    40             if(!inDeg[E[i].to]) q.push(E[i].to);
    41         }
    42     }
    43     printf("
    "); 
    44 }
    45 
    46 int main(){
    47     while(scanf("%d%d", &n, &m) != EOF){
    48         memset(E, 0, sizeof(E)); cnt = 0;
    49         memset(inDeg, 0, sizeof(inDeg));
    50         memset(fir, 0, sizeof(fir));
    51         for(int i=1; i<=m; i++){
    52             int x, y;
    53             scanf("%d%d", &x, &y);
    54             addEdge(x, y);
    55             inDeg[y]++;
    56         }
    57         Topsort();
    58     }
    59         
    60     return 0;
    61 }
  • 相关阅读:
    postgres 如何把多行数据,合并一行,返回json字符串
    linux 安装中文字体(生成图片中文乱码解决)
    postgis 自相交数据检测 修复
    C# Winform程序如何获取运行路径, 控制台也可以
    Excel: Access is denied
    change the theme in VS2005 or VS2008
    接下来的一点计划
    wordwrap, breakword
    T SQL + 正则表达式
    神奇的Css + DIV,滚动的Grid
  • 原文地址:https://www.cnblogs.com/sineagle/p/9097886.html
Copyright © 2011-2022 走看看