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.

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 3 1 2 1 3 3 2
Output
1 3 2 
Input
4 5 3 1 4 1 2 3 3 4 2 4
Output
4 1 2 3 
Input
5 4 3 1 2 1 2 3 4 5
Output
3 1 2 4 5 

  题目大意 给定一个有向无环图,用1~n为所有顶点标号,每个顶点的标号互不相同,如果有有一条边从v连向u,则v的标号应比u小,输出字典序最小的标号方案。

  poj有一道一样的题,题解请戳这里

Code

 /**
* Codeforces
* Problem#825E
* Accepted
* Time: 46ms
* Memory: 5300k
*/
#include <bits/stdc++.h>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
Edge(const int end = , const int next = -):end(end), next(next){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
vector<Edge> edge;
MapManager(){}
MapManager(int points):ce(){
h = new int[(const int)(points + )];
memset(h, -, sizeof(int) * (points + ));
}
inline void addEdge(int from, int end){
edge.push_back(Edge(end, h[from]));
h[from] = ce++;
}
inline void addDoubleEdge(int from, int end){
addEdge(from, end);
addEdge(end, from);
}
Edge& operator [] (int pos) {
return edge[pos];
}
inline void clear() {
edge.clear();
delete[] h;
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
#define m_endpos -1
///map template ends int n, m;
MapManager g;
int* dag;
int* dep; inline boolean init() {
if(!readInteger(n)) return false;
readInteger(m);
g = MapManager(n);
dag = new int[(n + )];
dep = new int[(n + )];
memset(dag, , sizeof(int) * (n + ));
for(int i = , a, b; i <= m; i++) {
readInteger(a);
readInteger(b);
g.addEdge(b, a);
dag[a]++;
}
return true;
} priority_queue<int> que;
inline void topu() {
for(int i = ; i <= n; i++)
if(!dag[i]) {
que.push(i);
}
int cnt = ;
while(!que.empty()) {
int e = que.top();
dep[e] = cnt++;
que.pop();
for(int i = m_begin(g, e); i != m_endpos; i = g[i].next) {
int& eu = g[i].end;
dag[eu]--;
if(!dag[eu])
que.push(eu);
}
}
} inline void solve() {
topu();
for(int i = ; i <= n; i++)
printf("%d ", n - dep[i]);
} int main() {
init();
solve();
return ;
}

Codeforces 825E Minimal Labels - 拓扑排序 - 贪心的更多相关文章

  1. Codeforces 825E - Minimal Labels

    825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...

  2. 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 ...

  3. BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心

    BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...

  4. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  5. 2019.01.20 bzoj5158 Alice&Bob(拓扑排序+贪心)

    传送门 短代码简单题. 题意简述:对于一个序列XXX,定义其两个伴随序列a,ba,ba,b,aia_iai​表示以第iii个数结尾的最长上升子序列长度,bib_ibi​表示以第iii个数开头的最长下降 ...

  6. HDU 4857 逃生(反向建边的拓扑排序+贪心思想)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  7. BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)

    题意 题目链接 Sol 非常妙的一道题. 首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的.因为当前的\(k\)大并不意味着后面的点\(k\)也大 但是在反图上按\(k\)从大到 ...

  8. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  9. [LOJ2114][HNOI2015]-菜肴制作-拓扑排序+贪心

    <题面> 一个蒟蒻的痛苦一天 在今天的节目集训中,麦蒙将带领大家学习9种错误的解题策略 $15\%$算法(看两个就往下走吧) 1> puts("Impossible!&qu ...

随机推荐

  1. 一步一步学Python(3) 基础补充

    最近在系统学习Python,以MOOC上面的一套Python3的课程为基础.本文主要总结一下基础部分的关键点. 1.python基本数据类型 2.python运算符 3.构建简洁高效的IDE环境 4. ...

  2. catalog start with + switch database to copy的妙用

    catalog start with + switch database to copy的妙用 环境:RHEL6.4 + Oracle 11.2.0.4 Primary RAC + Standby R ...

  3. 8.一个页面从输入 URL 到页面加载显示完成,这个过程中都发生了什么?

    注:这题胜在区分度高,知识点覆盖广,再不懂的人,也能答出几句, 而高手可以根据自己擅长的领域自由发挥,从URL规范.HTTP协议.DNS.CDN.数据库查询. 到浏览器流式解析.CSS规则构建.lay ...

  4. sqli-labs(五)——盲注(boolean盲注以及时间盲注)

    第八关: 没有查询信息,输入id=1' 报错 ,也没有报错信息,这里应该是个盲注 使用boolean的盲注吧 先判断boolean的盲注可行 输入id=1' and '1'='1' %23 页面正常 ...

  5. Mysql重连错误

    Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully rec ...

  6. windows下golang环境搭建

    (1)golang安装配置. 下载地址:https://www.golangtc.com/download 解压后直接配置系统环境变量path,加上go.exe所在文件的路径即可. 配置系统环境变量G ...

  7. JSP—连接池

    1:为什么要使用连接池? 解决频繁连接释放造成的资源浪费 2:配置好的数据库连接池也是以数据源DateSource的形式存, 连接池的实现类负责建立与数据库的连接. 3:使用连接池关闭资源的区别? 使 ...

  8. datetime处理日期和时间

    datetime.now() # 获取当前datetimedatetime.utcnow() datetime(2017, 5, 23, 12, 20) # 用指定日期时间创建datetime 一.将 ...

  9. git克隆远程项目并创建本地对应分支

    http://jingyan.baidu.com/article/19192ad83ea879e53e5707ce.html

  10. ResourceBundle与Properties读取配置文件

    ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...