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<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#define maxn 100010
using namespace std;
int d[maxn],a[maxn];
vector<int> vec[maxn];
priority_queue<int> 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<vec[p].size();j++)
			{
				int t=vec[p][j];
				d[t]--;
				if(d[t]==0)
					q.push(t);
			}
		}
		for(i=1;i<n;i++)
			printf("%d ",a[i]);
		printf("%d\n",a[n]);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值