E. Minimal Labels
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 label v should be smaller than label u. 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 ≤ 10^5, 1 ≤ m ≤ 10^5).
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号点重新编号,要求满足下面两个要求:
1.对于原来编号u,v的两点,若存在一条从u指向v的边,那么重新编号后,u的编号要小于v的编号。
2.重新编号后,新的编号恰好构成一个1~N的排列。从1到N,依次输出该节点的新编号。如果有多种编号方案,输出字典序最小的方案。
拓扑序列裸题。水,但是容易错。
唯一有思维量的地方就是如何保证新的编号是字典序最小的。
直接给出我的方法:
1.题目中给出边< x,y >,那么存边时存< y,x >,并记录入度。
2.执行Topsort标准操作,压入大根堆中而不是栈中。 此时从N到1,依次给取出的堆顶元素进行编号。
说明一下正确性:
1.算法保证了总是先把新编号大的元素新取出。这一点一般的Topsort就可以保证。这保证了序列的合法性,但没有保证字典序最小。
2.在当前入度为0的点中,取出的元素总是原编号最大的哪一个,那么在答案数组中出现的位置就越靠后。注意到我们是从大到小编号的,这样就保证了字典序最小。
主要可能在新编号和原编号上有点绕。理清思路就好。(然而三更半夜打比赛怎么容易理清思路……)
再解答一个小问题:
主要是在和六号@Mogician_Evian 交流之后想到的:这道题可以用小根堆做吗?
看起来可以!
可能的操作是:
1.题目中说什么,就怎么连边,并记录入度。
2.执行Topsort标准操作,压入小根堆中。 此时从1到N,依次给取出的堆顶元素进行编号。
可能的分析方法和上面的是类似的。
然而并不能!
为什么?看看这一组数据:
3 1
3 1
正解应该是
2 3 1
然而小根堆会输出
3 1 2
为什么?
我们要的并不是让新编号小的数尽量靠前,而是让答案数组靠前的数尽量小!
小根堆讨论就只是让新编号小的数尽量靠前了。
那么大根堆讨论为什么正确呢?
如果某一时刻堆里有多个元素,那么这些多出来的元素是可以在之任意时刻被选择的。这对于两种讨论方法都适用。
举原编号为1的元素为例说明。大根堆中,当1在某时刻入堆后,那么它一定能在之后讨论到的新编号更小的时候也被讨论到,直到优先队列中只剩它为止。换句话说,当堆里只有1时,当前的图中只存在一个入度为0的元素,那就是1。 根据讨论新编号从大到小的顺序,显然这是原编号1位置所能达到的最小编号了,即答案数组第一个元素的最小值了。
同理,可得这种算法在满足答案数组i位置数值最小后,也满足i+1位置数值最小。
写成小根堆之后,在不清醒的情况下很难发现问题。这道题能AC也算是我当时运气好吧。
//正解
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define MAXN 200005
#define MAXM 200005
using namespace std;
int N,M,D[MAXN],Ans[MAXN];
int en[MAXM],las[MAXN],nex[MAXM],tot;
void ADD(int x,int y)
{
en[++tot]=y;
nex[tot]=las[x];
las[x]=tot;
}
int main()
{
int i,x,y,j;
priority_queue<int>Q;
scanf("%d%d",&N,&M);
for(i=1;i<=M;i++)
{
scanf("%d%d",&x,&y);
D[x]++;
ADD(y,x);
}
for(i=1;i<=N;i++)if(D[i]==0)Q.push(i);
j=N;
while(Q.size())
{
x=Q.top();Q.pop();
Ans[x]=j--;
for(i=las[x];i;i=nex[i])
{
y=en[i];
if(D[y]>0)
{
D[y]--;
if(D[y]==0)Q.push(y);
}
}
}
for(i=1;i<=N;i++)printf("%d ",Ans[i]);
}
还是贴一下小根堆WA代码吧……也好做一个反面教材
//标准错误代码
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define MAXN 200005
#define MAXM 200005
using namespace std;
int N,M,D[MAXN],Ans[MAXN];
int en[MAXM],las[MAXN],nex[MAXM],tot;
void ADD(int x,int y)
{
en[++tot]=y;
nex[tot]=las[x];
las[x]=tot;
}
int main()
{
int i,x,y,j;
priority_queue<int>Q;
scanf("%d%d",&N,&M);
for(i=1;i<=M;i++)
{
scanf("%d%d",&x,&y);
D[y]++;
ADD(x,y);
}
for(i=1;i<=N;i++)if(D[i]==0)Q.push(-i);
j=N;
while(Q.size())
{
x=-Q.top();Q.pop();
Ans[x]=j--;
for(i=las[x];i;i=nex[i])
{
y=en[i];
if(D[y]>0)
{
D[y]--;
if(D[y]==0)Q.push(-y);
}
}
}
for(i=1;i<=N;i++)printf("%d ",Ans[i]);
}