This problem need to use topological sort of thinking, but the problem there is one condition - the lexicographically minimal, so you can use an increasing priority queue to maintain the degree of each find a point of 0 put it thus push into each Analyzing the current time point is always the point of a minimum of 0 lexicographically.
Code:
// Created by CAD on 2019/8/6.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f
#define PII pair<int,int>
#define PIII pair<pair<int,int>,int>
#define mst(name, value) memset(name,value,sizeof(name))
#define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)
#define test(n) cout<<n<<endl
using namespace std;
priority_queue<int> q;
const int maxn = 2e5 + 5;
int cnt[maxn];
vector<int> g[maxn];
int a[maxn];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1, u, v; i <= m; ++i)
{
cin >> u >> v;
g[v].emplace_back(u);
cnt[u]++;
}
for (int i = 1; i <= n; ++i) if (cnt[i] == 0) q.push(i);
int ans = n;
while (!q.empty())
{
int now = q.top();
q.pop();
a[now] = ans--;
for (auto i:g[now])
if (--cnt[i] == 0) q.push(i);
}
for (int i = 1; i <= n; ++i)
{
printf("%d%c", a[i], " \n"[i == n]);
}
}