Minimal Labels
这个题需要用到拓扑排序的思维,但是这个题还有一个条件——字典序最小,因此可以用一个递增的优先队列来维护,每找到一个入度为 0 的点就把它 push 进去因而每一次判断的点总是当前入度为 0 的字典序最小的点。
代码:
// Created by CAD on 2019/8/6.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f
#define PII pair<int,int>
#define PIII pair<pair<int,int>,int>
#define mst(name, value) memset(name,value,sizeof(name))
#define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)
#define test(n) cout<<n<<endl
using namespace std;
priority_queue<int> q;
const int maxn = 2e5 + 5;
int cnt[maxn];
vector<int> g[maxn];
int a[maxn];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1, u, v; i <= m; ++i)
{
cin >> u >> v;
g[v].emplace_back(u);
cnt[u]++;
}
for (int i = 1; i <= n; ++i) if (cnt[i] == 0) q.push(i);
int ans = n;
while (!q.empty())
{
int now = q.top();
q.pop();
a[now] = ans--;
for (auto i:g[now])
if (--cnt[i] == 0) q.push(i);
}
for (int i = 1; i <= n; ++i)
{
printf("%d%c", a[i], " \n"[i == n]);
}
}
Minimal Labels的更多相关文章
- 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 ...
- Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...
- Codeforces 825E - Minimal Labels
825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...
- Codeforces 825E Minimal Labels - 拓扑排序 - 贪心
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multi ...
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- 解题:CF825E Minimal Labels
题面 看起来似乎是个水水的拓扑排序+堆,然而并不对,因为BFS拓扑排序的话每次只会在“当前”的点中排出一个最小/大的字典序,而我们是要一个确定的点的字典序尽量小.正确的做法是反向建图,之后跑一个字典序 ...
- codeforce E - Minimal Labels+hdu 4857
两个题目的意思差不多 都是希望得出的拓扑序如果有多种 要求输出字典序小的情况 这里引用一个大佬的博客 关于为什么不能直接建图然后用小根堆解决这个问题(http://blog.csdn.net/rgno ...
- Centos 7 minimal install 无网络无ifconfig的解决
Centos7这个比较不厚道, minimal install下居然不带net-tools 先要连上网络 修改/etc/sysconfig/network-scripts/ifcfg-ens12312 ...
- 虚拟树Demos\Minimal 简单的例子
//分析虚拟树demo6-VirtualTreeView\VirtualTreeViewV5.3.0\Demos\Minimal的main.pas文件 unit Main; // Demonstrat ...
随机推荐
- 怎样重置MySQL密码?
systemctl stop mysqld systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" systemct ...
- 进阶Java编程(4)多线程深入话题
多线程深入话题 1,优雅的停止线程 在多线程操作之中如果要启动多线程使用的肯定是Thread类中的start()方法,而如果对于多线程需要进行停止处理,Thread类原本提供有stop()方法,但是这 ...
- python之jupyter notebook
jupyter是一种交互式计算和开发环境的笔记,ipython命令行比原生的python命令行更加友好和高效,还可以运行web版的界面,支持多语言,输出图形.音频.视频等功能. 安装 pip inst ...
- java基础3(异常)
1.异常的体系 1)请描述异常的继承体系 异常继承体系为:异常的根类是 java.lang.Throwable,其下有两个子类:java.lang.Error 与 java.util.Exceptio ...
- string字符串长度和字节长度问题
string str = "abcdef 安安安"; int i = str.Length; byte[] bt = System.Text.Encoding.Default.Ge ...
- 阿里P8架构师谈:MySQL慢查询优化、索引优化、以及表等优化总结
更多内容:https://www.toutiao.com/i6599796228886626829/?tt_from=weixin&utm_campaign=client_share& ...
- 8.Hibernate性能优化
性能优化 1.注意session.clear() 的运用,尤其在不断分页的时候 a) 在一个大集合中进行遍历,遍历msg,取出其中额含有敏感字样的对象 b) 另外一种形式的内存泄漏( //面试题:Ja ...
- D-Link系列路由器漏洞挖掘入门
D-Link系列路由器漏洞挖掘入门 前言 前几天去上海参加了geekpwn,看着大神们一个个破解成功各种硬件,我只能在下面喊 6666,特别羡慕那些大神们.所以回来就决定好好研究一下路由器,争取跟上大 ...
- three.js之让物体动起来方式(一)移动摄像机
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Codeforces Round #454 D. Power Tower (广义欧拉降幂)
D. Power Tower time limit per test 4.5 seconds memory limit per test 256 megabytes input standard in ...