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;
}

Codeforces 825E - Minimal Labels的更多相关文章

  1. Codeforces 825E Minimal Labels - 拓扑排序 - 贪心

    You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multi ...

  2. Educational Codeforces Round 25 E. Minimal Labels&&hdu1258

    这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...

  3. Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Minimal Labels

    Minimal Labels 这个题需要用到拓扑排序的思维,但是这个题还有一个条件--字典序最小,因此可以用一个递增的优先队列来维护,每找到一个入度为 0 的点就把它 push 进去因而每一次判断的点 ...

  5. Codeforces 797C - Minimal string

    C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...

  6. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  7. Codeforces 1092E Minimal Diameter Forest

    Minimal Diameter Forest 首先我们找出每个连通块中的特殊点, 特殊点的定义是到各种个连通块中距离的最大值最小的点, 每个连通块肯定通过特殊点连到其他连通块, 我们把有最大值的特殊 ...

  8. CF-825E Minimal Labels 反向拓扑排序

    http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...

  9. 解题:CF825E Minimal Labels

    题面 看起来似乎是个水水的拓扑排序+堆,然而并不对,因为BFS拓扑排序的话每次只会在“当前”的点中排出一个最小/大的字典序,而我们是要一个确定的点的字典序尽量小.正确的做法是反向建图,之后跑一个字典序 ...

随机推荐

  1. 《Cracking the Coding Interview》——第11章:排序和搜索——题目6

    2014-03-21 21:50 题目:给定一个MxN的二位数组,如果每一行每一列都是升序排列(不代表全展开成一个一维数组仍是升序排列的).请设计一个算法在其中查找元素. 解法:对于这么一个数组,有两 ...

  2. [转]如何像Python高手(Pythonista)一样编程

    本文转自:http://xianglong.me/article/how-to-code-like-a-pythonista-idiomatic-python 最近在网上看到一篇介绍Pythonic编 ...

  3. Dapper 学习笔记

    一.基础 1.Dapper代码就一个SqlMapper.cs文件, 前人测试Dapper速度较快 的Orm,读取速度接近IDataReader,超过DataTable. 2.a.下载地址 https: ...

  4. Percona-Tookit工具包之pt-slave-find

      Preface       If we want to check out how many slaves in our replication environment.We can execut ...

  5. Java SE 5.0 - SE 8 的功能增强

    Table of Contents 前言 Java 5.0 Generics Enhanced for Loop Autoboxing Typesafe Enums Varargs Static Im ...

  6. 5.0 Genymotion安装以及基础使用

    后续考虑到python+appium多设备并发执行,需要多台手机,所以这里就直接更新一个jenymotion,后续多设备执行直接用真机+模拟器操作!Genymotion第一步:百度搜索[Genymot ...

  7. python学习总结---函数使用 and 装饰器

    # 函数使用 ### 零碎知识 - 灵活的if-else ```python a = 3 if False else 5 print(a) ''' if False: a = 3 else: a = ...

  8. Leetcode 661.图片平滑器

    图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个 ...

  9. python完成留言板功能

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  10. intellij idea 2017 工具使用问题

    1.打开idea 打开maven项目报错:Unable to import maven project 2.在idea中Help->Show Log in Explorer->idea.l ...