Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图
1 second
256 megabytes
standard input
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 pair of vertices. Graph can be disconnected.
You should assign labels to all vertices in such a way that:
- Labels form a valid permutation of length n — an integer sequence such that each integer from 1 to n appears exactly once in it.
- If there exists an edge from vertex v to vertex u then labelv should be smaller than labelu.
- Permutation should be lexicographically smallest among all suitable.
Find such sequence of labels to satisfy all the conditions.
The first line contains two integer numbers n, m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105).
Next m lines contain two integer numbers v and u (1 ≤ v, u ≤ n, v ≠ u) — edges of the graph. Edges are directed, graph doesn't contain loops or multiple edges.
Print n numbers — lexicographically smallest correct permutation of labels of vertices.
3 3
1 2
1 3
3 2
1 3 2
4 5
3 1
4 1
2 3
3 4
2 4
4 1 2 3
5 4
3 1
2 1
2 3
4 5
3 1 2 4 5
题意:给你n个点,m条边的有向无环图,一条边u->v表示u的权值小于v的权值;求字典序最小的方案;
思路:反向建图,使得大的点的权值更大;类似与hdu 4857;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; vector<int>edge[N];
int du[N],ans[N];
priority_queue<int>q;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
edge[v].push_back(u);
du[u]++;
}
for(int i=;i<=n;i++)
if(!du[i])q.push(i);
int s=n;
while(!q.empty())
{
int x=q.top();
q.pop();
ans[x]=s--;
for(int i=;i<edge[x].size();i++)
{
int v=edge[x][i];
du[v]--;
if(!du[v])q.push(v);
}
}
for(int i=;i<=n;i++)
printf("%d ",ans[i]);
return ;
}
Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图的更多相关文章
- Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- HDU2647(拓扑排序+反向建图)
题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,因为尽可能的让老板少付钱,那么a的工资就是b的工资+1.能够确定关系为a>b,依据拓扑排序建边的原则是把"小于" ...
- 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 Five-In-a-Row(DFS)
题目网址:http://codeforces.com/contest/825/problem/B 题目: Alice and Bob play 5-in-a-row game. They have ...
- Educational Codeforces Round 25 A,B,C,D
A:链接:http://codeforces.com/contest/825/problem/A 解题思路: 一开始以为是个进制转换后面发现是我想多了,就是统计有多少个1然后碰到0输出就行,没看清题意 ...
- Educational Codeforces Round 25 C. Multi-judge Solving
题目链接:http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test 1 second ...
随机推荐
- BUAA 111 圆有点挤
题目描述 gg最近想给女友送两个精美的小礼品:两个底面半径分别为R1和R2的圆柱形宝石,并想装在一个盒子里送给女友. 好不容易找到了一个长方体的盒子,其底面为A*B的矩形,他感觉好像宝石装不进去,但又 ...
- php_study progress(1)
PHP是一种语法简单.功能强大的网络编程语言.在语法格式上,PHP借鉴了广泛流行的C.Java和Perl等编程语言的特点,非常类似于C语言,但比C语言更简单,易学和易用,因此特别适合于学习过C语言,有 ...
- The Little Prince-12/03
The Little Prince-12/03 These days, I am always busy with my things, including experiment and others ...
- webVR框架A-frame
A-frame:https://blog.csdn.net/sun124608666/article/details/77869570 three.js学习文档:http://www.hewebgl. ...
- bzoj4861 / P3715 [BJOI2017]魔法咒语
P3715 [BJOI2017]魔法咒语 AC自动机+dp+矩阵乘法 常规思路是按基本串建立AC自动机 然鹅这题是按禁忌串建立AC自动机 对后缀是禁忌的点以及它的失配点做上标记$(a[i].ed)$, ...
- centos 6.5 gdb 7.10安装make[5]: *** [install-bfdincludeHEADERS] Error 1解决
make[5]: *** [install-bfdincludeHEADERS] Error 1make[5]: Leaving directory `/usr/local/gdb-7.10/bfd' ...
- Deep Learning for NLP
Deep Learning for NLP The First Paper Proposed Bi-LSTM+CRF 我认为,第一篇提出 Bi-LSTM+CRF 架构的文章是: Huang Z, Xu ...
- How To Answer The Question "tell me about yourself" In An Interview
Two or three minutes. ponit list: education experience highlight accomplishments show passion/ drive ...
- String的getBytes()方法 以及 new String()
在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组.这表示在不同的操作系统下,返回的东西不一样! String.getBytes(Stringdecode) ...
- 创建一个maven项目
创建父工程 1.新建maven project,点击next 2.默认配置,点击next 3.默认配置,点击next 4.填写Group Id一般采用域名倒写,Artifact Id为项目名称.然后点 ...