hdoj 1085 確定比賽名次 【拓撲排序】

來源:互聯網
上載者:User

標籤:拓撲排序

中文題,不解釋。

這是我的第一道拓撲排序題,先來講一下什麼是拓撲排序:

拓撲排序其實就是如果要進行某一項活動的時候,它的基礎活動要先進行。比如說,學機率論之前必須要學會高等數學,那麼高等數學就是學機率論的前提條件,這就牽涉到先後課程怎麼學習,就是誰先學誰後學習, 拓撲排序就是解決這類問題的。

題目連結 http://acm.hdu.edu.cn/showproblem.php?pid=1285

代碼:

#include<stdio.h>#include<string.h>bool map[507][507];int in[507];//類比隊列int main(){int n, m, a, b, i;while(scanf("%d%d", &n, &m) == 2){memset(map, 0, sizeof(map));memset(in, 0, sizeof(in));while(m --){scanf("%d%d", &a, &b);if(!map[a][b]){map[a][b] = 1;in[b]++;}}int top, t;for(i = n; i > 0; i --){ //這個不能從1開始,題目要求是輸出順序最小的序列if(!in[i]){top = i;}}t = 1;while(t < n){printf("%d ", top);in[top] --;t++;for(i = 1; i <= n; i ++){if(map[top][i]){in[i]--;}}for(i = n; i > 0; i --){//這裡也是if(!in[i]){top = i;}}}printf("%d\n", top);}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.