zoukankan      html  css  js  c++  java
  • Codeforces 825E

    825E - Minimal Labels

    题意

    给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号。如果有边 ((u, v)) 那么 (label_u < label_v) 。要求最后字典序尽可能小。

    分析

    拓扑排序的变形。
    这题要统计的是每个点的出度,比如说某个点出度为 0 ,那么它的标号一定很大(因为它不需要比别的点小了),又要求字典序最小,对于初始图,那么出度为 0 的点且序号最大的点的标号一定为 n。优先队列维护下(最大值优先),如果某个点的标号确定了,那么删除所有连向这个点的边,更新出度。

    code

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e6 + 10;
    int n, m;
    vector<int> rG[MAXN];
    vector<int> topolist;
    priority_queue<int, vector<int>, less<int> > Q;
    int a[MAXN], c[MAXN];
    int main() {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            c[x]++;
            rG[y].push_back(x);
        }
        for(int i = 1; i <= n; i++) if(c[i] == 0) Q.push(i);
        int N = n;
        while(!Q.empty()) {
            int x = Q.top(); Q.pop();
            a[x] = N--;
            for(int i = 0; i < rG[x].size(); i++) {
                if(--c[rG[x][i]] == 0) Q.push(rG[x][i]);
            }
        }
        for(int i = 1; i <= n; i++) {
            printf("%d ", a[i]);
        }
        return 0;
    }
    
  • 相关阅读:
    java.lang.IllegalArgumentException: node to traverse cannot be null!
    c3p0连接池的使用
    eclipse插件
    eclipse字体颜色设置
    oracle增删改查
    resultMap / resultType
    oracle 序列 ,check约束
    JSP:一种服务器端动态页面技术的组件规范。
    js
    字体
  • 原文地址:https://www.cnblogs.com/ftae/p/7219763.html
Copyright © 2011-2022 走看看