CodeForces - 825E CodeForces - 825E

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 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 nm (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 
题意:给出n个点,m条边组成的有向图,让你对图中的每个点进行标记

要求:1.有1~n个点,就用1~n标记,每个数只能使用一次

   2.如果v有边指向u,那么v的标记比u小。

   3.在1,2的前提下输出符合字典顺序的标记。

思路:拓扑排序加优先队列,当图不是连通图时,优先队列能够保证最后的输出是字典序

#include
#include
#include
#include
#define maxn 100010
using namespace std;
int d[maxn],a[maxn];
vector vec[maxn];
priority_queue q;
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		
		int i,j;
		memset(d,0,sizeof(d));
		while(m--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			d[u]++;
			vec[v].push_back(u);
		}
		for(i=1;i<=n;i++)
			if(d[i]==0)
				q.push(i);
		for(i=n;q.size();)
		{
			int p=q.top();
			q.pop();
			a[p]=i;
			i--;
			for(j=0;j

你可能感兴趣的:(简单算法)