Codeforces 825E - Minimal Labels
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的更多相关文章
- Codeforces 825E Minimal Labels - 拓扑排序 - 贪心
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multi ...
- Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...
- 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 ...
- Minimal Labels
Minimal Labels 这个题需要用到拓扑排序的思维,但是这个题还有一个条件--字典序最小,因此可以用一个递增的优先队列来维护,每找到一个入度为 0 的点就把它 push 进去因而每一次判断的点 ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Codeforces 1092E Minimal Diameter Forest
Minimal Diameter Forest 首先我们找出每个连通块中的特殊点, 特殊点的定义是到各种个连通块中距离的最大值最小的点, 每个连通块肯定通过特殊点连到其他连通块, 我们把有最大值的特殊 ...
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- 解题:CF825E Minimal Labels
题面 看起来似乎是个水水的拓扑排序+堆,然而并不对,因为BFS拓扑排序的话每次只会在“当前”的点中排出一个最小/大的字典序,而我们是要一个确定的点的字典序尽量小.正确的做法是反向建图,之后跑一个字典序 ...
随机推荐
- 设计模式之第22章-组合模式(Java实现)
设计模式之第22章-组合模式(Java实现) “鱼哥,有没有什么模式是用来处理树形的“部分与整体”的层次结构的啊.”“当然”“没有?”“有啊.别急,一会人就到了.” 组合模式之自我介绍 “请问你是?怎 ...
- dynamic基元类型与隐式类型的局部变量var
dynamic代码示例 using System; using System.Collections.Generic; using System.Linq; using System.Text; na ...
- MySQL隐式转换测试
Preface There're various data type in MySQL such as number,string,date,time,lob,etc.The data ...
- PhpStorm快捷键设置/个性化设置,如何多项目共存?如何更换主题?
#常用快捷键 设置快捷键:File -> Settings -> IDE Settings -> Keymap -> 选择“Eclipse” -> 然后“Copy”一份 ...
- table内容超出宽度时隐藏并显示省略标记
HTML中,一个表格,要达到二个条件: 1.内容多了不自动换行: 2.固定单元格宽度.如果内容超出,则隐藏: 如 果在IE下,只是写成<table style="table-layou ...
- PHP面向对象练习2
思路:构造函数完成数据库连接,增删改一个方法,查询一条记录一个方法,查询多条一个方法,sql执行失败则返回提示,并交出sql语句方便查错 代码: <?class dbcontroll{ priv ...
- 多线程和CPU的关系
什么是CPU (1) Central Progressing Unit 中央处理器,是一块超大规模的集成电路,是一台计算机的运算核心和控制核心. (2) CPU包括 ...
- vue cli 3 & webpack-merge & webpack 3 & bug
vue cli 3 & webpack-merge & webpack & bug bug webpack-merge & bug webpack-merge ??? ...
- 51nod 1040 最大公约数之和 | 数论
给出一个n,求1-n这n个数,同n的最大公约数的和 n<=1e9 考虑枚举每个因数,对答案贡献的就是个数*大小
- myeclipse maven web项目配置
启用maven:window-->preference-->MyEclipse-->Maven4MyEclipse, 勾选复选框(Enable Mave4MyEclipse feat ...