zoukankan      html  css  js  c++  java
  • CF-825E Minimal Labels (反向拓扑)

    题意:就是给出n点m边,输出排序使并且输出的字典序最小 (注意是这里是输出的序号而不是值).

    思路:这类对于拓扑排序字典序最小的问题使用逆向拓扑排序。

    完整代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e6 + 10;
    int n, m;
    vector<int> graph[MAXN];
    priority_queue<int, vector<int>, less<int> > Q;
    int a[MAXN], indegree[MAXN];
    int main() {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            indegree[x]++;//把出度看出入度 
            graph[y].push_back(x);//反向建立图 
        }
        for(int i = 1; i <= n; i++) 
            if(indegree[i] == 0) Q.push(i);
        int t = n;
        while(!Q.empty()) {
            int u  = Q.top(); Q.pop();
            a[u] = t--;//越大的值标号越大
            for(int i = 0; i <  graph[u].size(); i++) {
                if(--indegree[ graph[u][i]] == 0) Q.push( graph[u][i]);
            }
        }
        for(int i = 1; i <= n; i++) {
            printf("%d ", a[i]);
        }
        return 0;
    }
  • 相关阅读:
    hdu 6201 dfs
    Oulipo POJ
    Kitchen Measurements UVALive
    Surf Gym
    hoj 13969 Racing Gems
    分块
    分块学习资料
    Jam's problem again HDU
    树的点分治
    Census UVA
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11230922.html
Copyright © 2011-2022 走看看