• 杭电1285--确定比赛名次(拓扑排序)


    确定比赛名次

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


    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   |   We have carefully selected several similar problems for you:  2647 3342 1811 1548 1269 
    数据较少可用邻接矩阵;
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 int map[550][550], indegree[550], result[550];
     6 int n, m;
     7 void Tsort(){
     8     int m, k = 0;
     9     for(int i = 0; i < n; i++){
    10         //*m = -1; 
    11         for(int j = 1; j <= n; j++){
    12             if(!indegree[j]){
    13                 m = j; break;
    14             }
    15         }    
    16            result[k++] = m; indegree[m]--;  //删点; 
    19         for(int j = 1; j <= n; j++)      //删边; 
    20             if(map[m][j])
    21                 --indegree[j]; 
    22     } 
    23     for(int i = 0; i < k; i++){
    24         if(i != 0)
    25             printf(" %d", result[i]);
    26         else
    27             printf("%d", result[i]);
    28     }
    29     printf("
    ");
    30 }
    31 int main()
    32 {
    33     while(~scanf("%d %d", &n, &m))
    34     {
    35         memset(map, 0, sizeof(map));
    36         memset(indegree, 0, sizeof(indegree));
    37         for(int i = 0; i < m; i++)
    38         {
    39             int a, b;
    40             scanf("%d %d", &a, &b);
    41             if(!map[a][b]){   //去重; 
    42                 map[a][b] = 1; 
    43                 indegree[b]++;
    44             }
    45         }
    46         Tsort();
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    WebAssembly API & MDN All In One
    Apple iPhone 14 Pro 药丸设计看不懂,药丸上面那条屏幕缝隙完全没有用处呀?
    API 调试工具 All In One
    macOS Time Machine All In One
    使用 macOS 输入多个空格,会自动添加一个点 bug All In One
    手把手的教你如何使用 Vite 搭建一个 React UI 组件库 All In One
    LeetCode 两数相加算法题解 All In One
    如何把 iPad 作为 Mac 扩展屏幕使用 All In One
    【DEMO】【C/C++】最简单的一种回调函数
    Qt中cvMat与QImage,QPixmap的转换
  • 原文地址:https://www.cnblogs.com/soTired/p/4729384.html
Copyright © 2020-2023  润新知