UVA - 10305 Ordering Tasks(拓撲排序)

來源:互聯網
上載者:User

標籤:eps   stack   lib   ack   map   const   iostream   task   while   

題意:給定優先關係進行拓撲排序。

分析:將入度為0的點加入優先隊列,並將與之相連的點入度減1,若又有度數為0的點,繼續加入優先隊列,依次類推。

#pragma comment(linker, "/STACK:102400000, 102400000")#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define Min(a, b) ((a < b) ? a : b)#define Max(a, b) ((a < b) ? b : a)typedef long long ll;typedef unsigned long long llu;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const ll LL_INF = 0x3f3f3f3f3f3f3f3f;const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1e9 + 7;const double pi = acos(-1.0);const double eps = 1e-8;const int MAXN = 100 + 10;const int MAXT = 10000 + 10;using namespace std;int in[MAXN];vector<int> a[MAXN];vector<int> ans;priority_queue<int, vector<int>, greater<int> > q;int main(){    int n, m;    while(scanf("%d%d", &n, &m) == 2){        if(!n && !m) return 0;        memset(in, 0, sizeof in);        ans.clear();        for(int i = 0; i < MAXN; ++i) a[i].clear();        while(m--){            int x, y;            scanf("%d%d", &x, &y);            a[x].push_back(y);            ++in[y];        }        for(int i = 1; i <= n; ++i){            if(in[i] == 0){                q.push(i);            }        }        while(!q.empty()){            int t = q.top();            q.pop();            ans.push_back(t);            int len = a[t].size();            for(int i = 0; i < len; ++i){                if(--in[a[t][i]] == 0){                    q.push(a[t][i]);                }            }        }        int len = ans.size();        for(int i = 0; i < len; ++i){            if(i) printf(" ");            printf("%d", ans[i]);        }        printf("\n");    }    return 0;}

 

UVA - 10305 Ordering Tasks(拓撲排序)

聯繫我們

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