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

    确定比赛名次




    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
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int G[505][505];
     9 int indegree[505];
    10 int topo[505];
    11 int t,n,m;
    12 void toposort()
    13 {
    14     t=0;
    15     priority_queue<int,vector<int>,greater<int> >q;
    16     for(int i=1;i<=n;i++)
    17     {
    18         if(indegree[i]==0)
    19         q.push(i);
    20     }
    21     int temp;
    22     while(!q.empty())
    23     {
    24         temp=q.top();
    25         topo[t++]=temp;
    26         q.pop();
    27         for(int i=1;i<=n;i++)
    28         {
    29             if(G[temp][i])
    30             {
    31                 indegree[i]--;
    32                 if(indegree[i]==0)
    33                 q.push(i);
    34             }
    35         }
    36     }
    37 }
    38 
    39 
    40 int main()
    41 {
    42     //freopen("in.txt","r",stdin);
    43     int i,a,b;
    44     while(scanf("%d%d",&n,&m)!=EOF)
    45     {
    46         memset(indegree,0,sizeof(indegree));
    47         memset(G,0,sizeof(G));
    48         for(i=0;i<m;i++)
    49         {
    50             scanf("%d%d",&a,&b);
    51             if(!G[a][b])
    52             indegree[b]++;
    53             G[a][b]=1;
    54         }
    55         toposort();
    56         for(i=0;i<n-1;i++)
    57         printf("%d ",topo[i]);
    58         printf("%d
    ",topo[i]);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    学习:ASP.NET中App_Code,App_Data等文件夹的作用(转)
    总结:CLR Via C#(第九章):属性与索引器
    总结:CLR Via C#(第八章):其他(方法传递、out、ref)
    Init Version
    Code 128 Barcode Font Advantage Package 的常见问题
    Skelta Workflow.NET 2004 R2
    GTP.NET 甘特图项目管理控件
    Code 128 Barcode Font Advantage Package 中如何手动加入起始符,结束符,校验符
    VectorDraw Professional v5.1.1.1043
    开篇
  • 原文地址:https://www.cnblogs.com/homura/p/4727536.html
Copyright © 2011-2022 走看看