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): 32544    Accepted Submission(s): 12755
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285

    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
     一题简单的拓扑排序基础题,还有一题为hdu2647
    代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #define inf 1<<29
     7 using namespace std;
     8 queue<int> q;
     9 int map[550][550];
    10 int in[550],a[550];
    11 int main()
    12 {
    13     int n,m;
    14     while(scanf("%d%d",&n,&m)!=EOF)
    15     {
    16         memset(map,0,sizeof(map));
    17         memset(in,0,sizeof(in));
    18         memset(a,0,sizeof(a));
    19         int p,tot=0,t=n-1;
    20         for(int i=0;i<m;i++)
    21         {
    22             int u,v;
    23             scanf("%d%d",&u,&v);
    24             map[u][v]++;
    25             in[v]++;
    26         }
    27         for(int i=1;i<=n;i++)
    28             if(in[i]==0)
    29             {
    30                 cout<<i;
    31                 in[i]=inf;
    32                 p=i;
    33                 break;
    34             }
    35         while(t--)
    36         {    
    37             for(int i=1;i<=n;i++)
    38             {
    39                 if(map[p][i]>0)
    40                 {
    41                     in[i]-=map[p][i];
    42                     map[p][i]=0;
    43                 }
    44             }
    45             for(int i=1;i<=n;i++)
    46                 if(in[i]==0)
    47                 {
    48                     cout<<" "<<i;
    49                     in[i]=inf;
    50                     p=i;
    51                     break;
    52                 }
    53         }
    54         cout<<endl;
    55     }
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/xiongtao/p/8809012.html
Copyright © 2011-2022 走看看