Language: C++

/******************
*  Author: BD747  *
******************/
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

int N,M;
int A[100005],B[100005];
vector<int> graph[100005];
int indeg[100005];
int dmax[100005];

int visit(int p){
	if(dmax[p]!=-1)return dmax[p];
	
	dmax[p]=0;
	for(int q : graph[p]){
		dmax[p]=max(dmax[p],visit(q)+1);
	}
	
	return dmax[p];
}

int ans[100005];

void visit2(int p,int rt){
	if(ans[p]!=-1)return;
	ans[p]=rt+1;
	vector<pair<int,int> > ch;
	for(int q:graph[p])ch.push_back({dmax[q],q});
	sort(ch.begin(),ch.end());
	reverse(ch.begin(),ch.end());
	
	for(auto a:ch)visit2(a.second,p);
}

int main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin>>N>>M;
	for(int i=0;i<N+M-1;i++){
		cin>>A[i]>>B[i];
		A[i]--;B[i]--;
		graph[A[i]].push_back(B[i]);
		++indeg[B[i]];
	}
	int ori=-1;
	for(int i=0;i<N;i++){
		if(indeg[i]==0)ori=i;
	}
	for(int i=0;i<N;i++)dmax[i]=ans[i]=-1;
	visit(ori);
	visit(ori,-1);
	for(int i=0;i<N;i++)cout<<ans[i]<<endl;

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:68:14: error: no matching function for call to 'visit(int&, int)'
   68 |         visit(ori,-1);
      |         ~~~~~^~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:16:
/usr/include/c++/14/variant:1855:5: note: candidate: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
/usr/include/c++/14/variant:1855:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/variant: In substitution of 'template<class _Visitor, class ... _Variants> using std::__detail::__variant::__visit_result_t = std::invoke_result_t<_Visitor, std::__detail::__variant::__get_t<0, _Variants, decltype (std::__detail::__variant::__as(declval<_Variants>())), typename std::variant_alternative<0, typename std::remove_reference<decltype (std::__detail::__variant::__as(declval<_Variants>()))>::type>::type>...> [with _Visitor = int&; _Variants = {int}]':
/usr/include/c++/14/variant:1855:5:   required by substitution of 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...) [with _Visitor = int&; _Variants = {int}]'
a.cc:68:7:   required from here
   68 |         visit(ori,-1);
      |         ~~~~~^~~~~~~~
/usr/include/c++/14/variant:1115:47: error: no matching function for call to '__as(int)'
 1115 |       typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
      |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/variant:192:5: note: candidate: 'template<class ... _Types> constexpr std::variant<_Types ...>& std::__detail::__variant::__as(std::variant<_Types ...>&)'
  192 |     __as(std::variant<_Types...>& __v) noexcept
      |     ^~~~
/usr/include/c++/14/variant:192:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/variant:1115:47: note:   mismatched types 'std::variant<_Types ...>' and 'int'
 1115 |       typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
      |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/variant:197:5: note: candidate: 'template<class ... _Types> constexpr const std::variant<_Types ...>& std::__detail::__variant::__as(const std::variant<_Types ...>&)'
  197 |     __as(const std::variant<_Types...>& __v) noexcept
      |     ^~~~
/usr/include/c++/14/variant:197:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/variant:1115:47: note:   mismatched types 'const std::variant<_Types ...>' and 'int'
 1115 |       typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
      |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/variant:202:5: note: candidate: 'template<class ... _Types> constexpr std::variant<_Types ...>&& std::__detail::__variant::__as(std::variant<_Types ...>&&)'
  202 |     __as(std::variant<_Types...>&& __v) noexcept
      |     ^~~~
/usr/include/c++/14/variant:202:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/variant:1115:47: note:   mismatched types 'std::variant<_Types ...>' and 'int'
 1115 |       typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
      |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/variant:207:5: note: candidate: 'template<class ... _Types> constexpr const std::variant<_Types ...>&& std::__detail::__variant::__as(const std::variant<_Types ...>&&)'
  207 |     __as(const std::variant<_Types...>&& __v) noexcept
      |     ^~~~
/usr/include/c++/14/variant:207:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/variant:1115:47: note:   mismatched types 'const std::variant<_Types ...>' and 'int'
 1115 |       typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
      |                                ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:5: note: candidate: 'int visit(int)'
   25 | int visit(int p){
      |     ^~~~~
a.cc:25:5: note:   candidate expects 1 argument, 2 provided