CF825E:Minimal Labels(拓扑排序)
来源:互联网 发布:十香cosplay淘宝 编辑:程序博客网 时间:2024/06/05 09:20
E. Minimal Labels
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.
Input
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.
Output
Print n numbers — lexicographically smallest correct permutation of labels of vertices.
Examples
input
3 31 21 33 2
output
1 3 2
input
4 53 14 12 33 42 4
output
4 1 2 3
input
5 43 12 12 34 5
output
3 1 2 4 5
思路:先对点拓扑排序,因为要字典序最小,所以要将边全部反向,贪心从大往小编号,即大数能往后就往后。
# include <bits/stdc++.h>using namespace std;const int maxn = 1e5+3;int n, m, Next[maxn], in[maxn]={0}, ans[maxn],cnt=0;struct node{ int v, next;}edge[maxn<<1];void add_edge(int u, int v){ edge[cnt] = {v, Next[u]}; Next[u] = cnt++;}void solve(int sum){ priority_queue<int>q; for(int i=1; i<=n; ++i) if(in[i]==0) { --in[i]; q.push(i); } while(!q.empty()) { int u = q.top(); q.pop(); ans[u] = sum--; for(int i=Next[u]; i!=-1; i=edge[i].next) { int v = edge[i].v; if(--in[v] == 0) q.push(v); } }}int main(){ memset(Next, -1, sizeof(Next)); scanf("%d%d",&n,&m); while(m--) { int a, b; scanf("%d%d",&a,&b); ++in[a]; add_edge(b, a); } solve(n); for(int i=1; i<=n; ++i) printf("%d%c",ans[i],i==n?'\n':' '); return 0;}
阅读全文
0 0
- CF825E:Minimal Labels(拓扑排序)
- codeforces825E Minimal Labels【拓扑排序】
- Codeforces 825 F Minimal Labels(反向拓扑排序)
- CF-Educational-25 E-Minimal Labels (拓扑排序,字典序)
- Educational Codeforces Round 25 E. Minimal Labels(拓扑排序)
- Educational Codeforces Round 25E. Minimal Labels(拓扑排序+思维)
- codeforce 825E. Minimal Labels 拓扑排序 贪心思想
- cf 825E Minimal Labels 【拓扑】
- [拓扑序] Educational Codeforces Round 25 825E. Minimal Labels
- HDU 4857 逃生 && Codeforces 825 E. Minimal Labels 逆向拓扑序+优先队列
- codeforces 825 E Minimal Labels
- Codeforces 825E Minimal Labels
- Educational Codeforces Round 25 E. Minimal Labels
- Educational Codeforces Round 25 E. Minimal Labels
- 拓扑排序 (java)
- 拓扑排序(转)
- 拓扑排序(图论)
- 拓扑排序(谢庆皇)
- Scala---Akka Actor(一)tutorial-2
- C++虚函数及虚函数表在内存中的分配和访问
- 随笔一
- [日常套路] Boyer–Moore majority vote algorithm
- 1003 最大连续子序列
- CF825E:Minimal Labels(拓扑排序)
- HDU— 5968 异或密码
- android 服务
- HDU-2064-汉诺塔III
- Android Arcgis(13)、Gdal简单写个shp文件
- Jdbc事务笔记
- torch mac openmp
- 爬虫中的'伪装者'--header
- 初学者的linux