BestCoder #1-1 HDU 4857 逆拓扑排序

来源:互联网 发布:开票软件任务是什么 编辑:程序博客网 时间:2024/06/10 04:28
////  main.cpp//  HDU 4857 拓扑排序////  Created by 郑喆君 on 8/9/14.//  Copyright (c) 2014 itcast. All rights reserved.//#include<cstdio>#include<cstring>#include<iostream>#include<iomanip>#include<queue>#include<cmath>#include<stack>#include<map>#include<vector>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int int_max = 0x07777777;const int int_min = 0x80000000;const int maxn = 31000;struct cmp {    bool operator()(int a, int b){        return a < b;    }};vector<int> g[maxn];int cnt[maxn];int n,m;int main(int argc, const char * argv[]){    int t;    scanf("%d", &t);    while(t--){        scanf("%d %d", &n, &m);        for(int i = 1; i <= n; i++) g[i].clear();        memset(cnt, 0, sizeof(cnt));        for(int i = 0; i < m; i++){            int x,y;            scanf("%d %d", &x, &y);            g[y].push_back(x);            cnt[x]++;        }        priority_queue<int, vector<int>, cmp> q;        for(int i = 1; i <= n; i++) if(!cnt[i]) q.push(i);        vector<int> res;        while (!q.empty()) {            int u = q.top();            q.pop();            res.push_back(u);            for(int i = 0; i < g[u].size(); i++) if(--cnt[g[u][i]] == 0) q.push(g[u][i]);        }        if(n==1){            printf("%d\n", 1);        }else{            for(int i = res.size()-1; i > 0; i--) printf("%d ", res[i]);            printf("%d\n", res[0]);        }    }    }

0 0
原创粉丝点击