解题:CF825E Minimal Labels】的更多相关文章

题面 看起来似乎是个水水的拓扑排序+堆,然而并不对,因为BFS拓扑排序的话每次只会在“当前”的点中排出一个最小/大的字典序,而我们是要一个确定的点的字典序尽量小.正确的做法是反向建图,之后跑一个字典序最大的拓扑序.因为拓扑排序是一个类似于“解锁”的过程,它具有后效性,我们并不能直接贪心来做,需要反过来做消除后效性. #include<queue> #include<cstdio> #include<cstring> #include<algorithm> u…
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字典序增大.而反向拓扑排序在上述情况,只会使大序号更晚分配到序号,而小序号还是较小的,根据字典序比对规则,当然是选择反向拓扑排序啦. #include <iostream> #include <queue> #include <cstdio> #include <vec…
E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any p…
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educational Codeforces Round 25  E. Minimal Labels 题目链接:http://codeforces.com/contest/825/problem/E 题意:给你一个有向无环图(DAG),有n个顶点,m条边,顶点的序号分别是1-n,现在给你1-n的数对n个顶点进行赋值…
825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \(label_u < label_v\) .要求最后字典序尽可能小. 分析 拓扑排序的变形. 这题要统计的是每个点的出度,比如说某个点出度为 0 ,那么它的标号一定很大(因为它不需要比别的点小了),又要求字典序最小,对于初始图,那么出度为 0 的点且序号最大的点的标号一定为 n.优先队列维护下(最大值…
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 0…
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected. You should assign labels to all vertices in such a way that: Labels form a valid pe…
两个题目的意思差不多 都是希望得出的拓扑序如果有多种 要求输出字典序小的情况 这里引用一个大佬的博客 关于为什么不能直接建图然后用小根堆解决这个问题(http://blog.csdn.net/rgnoH/article/details/75253355 : 出处) 再解答一个小问题: 主要是在和六号@Mogician_Evian 交流之后想到的:这道题可以用小根堆做吗? 看起来可以! 可能的操作是: 1.题目中说什么,就怎么连边,并记录入度. 2.执行Topsort标准操作,压入小根堆中. 此时…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/problems/partition-labels/description/ 题目描述 A string S of lowercase letters is given. We want to partition this string into as many parts as possible so…
1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only cond…