HDU 1285 topological_sort

來源:互聯網
上載者:User

標籤:des   style   java   color   os   io   資料   for   

確定比賽名次
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11739 Accepted Submission(s): 4671


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
杭電ACM集訓隊訓練賽(VII)


Recommend
lcy
<span style="color:#6633ff;">
/**********************************************      author   :    Grant Yuan      time     :    2014.7.29      algorithm:    topological_sort      source   :    HDU 1285**********************************************/#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#define MAX 511using namespace std;int mat[MAX][MAX],num[MAX];int n,m,ans,sum;int lu[MAX];int main(){   int a,b;    while(~scanf("%d%d",&n,&m)){        memset(mat,0,sizeof(mat));        memset(num,0,sizeof(num));        memset(lu,0,sizeof(lu));        for(int i=0;i<m;i++)        {           scanf("%d%d",&a,&b);           if(mat[b][a]==0)           {               mat[b][a]=1;               num[b]++;           }        }        int i,p=1;        while(1){            for(i=1;i<=n;i++)            {                if(num[i]==0)                    break;            }            if(i==n+1)                break;                num[i]=-1;            lu[p++]=i;            for(int j=1;j<=n;j++)            {                if(mat[j][i])                    num[j]--;            }        }                for(int j=1;j<=n;j++)            if(j<n)printf("%d ",lu[j]);            else printf("%d",lu[j]);        printf("\n");    }    return 0;}</span>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.