• UVA 10305 Ordering Tasks(拓扑排序)


    题意:给定优先关系进行拓扑排序。

    分析:将入度为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;
    }
  • 相关阅读:
    新概念英语(1-121)The man in a hat
    新概念英语(1-119)who call out to the thieves in the dark?
    画像分析(1-1)如何为客户画像?
    大数据分析师
    英语词汇(2)fall down,fall off和fall over
    把梳子卖给和尚的故事
    洛谷P1970 花匠(dp)
    2018.10.24模拟赛2解题报告
    2018.10.24模拟赛1解题报告
    洛谷P1941 飞扬的小鸟(背包 dp)
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6274850.html
Copyright © 2020-2023  润新知