Language: C++

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <climits>
#include <algorithm>
#include <cassert>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <numeric>
#define ALL(c) (c).begin(), (c).end()

using namespace std;
using ll = long long;
using pli = pair<ll, int>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdi = pair<double, int>;
using pdd = pair<double, double>;

// Graph.h start--------------------------------------------
#pragma once

#include <vector>

using Weight = long long;

struct Vertex;
using Vertices = std::vector<Vertex *>;
struct Edge;
using Edges = std::vector<const Edge *>;

struct Vertex {
	const int id;
	Edges edges;
	Vertex(int id);
	void add_edge(const Edge *const p_e);
	void add_edge(const Edges &es);
};

struct Edge {
	Vertex *p_src, *p_dst;
	Weight weight;
	
	Edge(Vertex *p_src, Vertex *p_dst, Weight weight);
	Edge(const Edge &e);
	Edge& operator = (const Edge &de);
};

struct Graph {
	Vertices vertices;
	Edges edges;
	Graph(int nvertices);
	~Graph();
	void add_edge(Edge *p_edge);
	void add_edge(int edge_src, int edge_dst, Weight w);
	//void copy_vertex(int id_original, int id_copy);
};

Vertex::Vertex(int id) : id(id) { }
void Vertex::add_edge(const Edge *const p_e) {
	edges.push_back(p_e);
}
void Vertex::add_edge(const Edges &es) {
	for (const auto &p_e : es) {
		add_edge(p_e);
	}
}

//----------------------------------------------------------

using namespace std;

Edge::Edge(Vertex *p_src, Vertex *p_dst, Weight weight) : p_src(p_src), p_dst(p_dst), weight(weight) { }
Edge::Edge(const Edge &e) : Edge(e.p_src, e.p_dst, e.weight) {}
Edge& Edge::operator = (const Edge &e) {
	p_src = e.p_src;
	p_dst = e.p_dst;
	weight = e.weight;
	return *this;
}
bool operator < (const Edge &e1, const Edge &e2) {
	return e1.weight != e2.weight ? e1.weight > e2.weight : // !!INVERSE!!
		e1.p_src != e2.p_src ? e1.p_src->id < e2.p_src->id : e1.p_dst->id < e2.p_dst->id;
}

Graph::Graph(int nvertices) {
	for (int id = 0; id < nvertices; id++) {
		vertices.push_back(new Vertex(id));
	}
}
Graph::~Graph() {
	for (auto &p_vertex : vertices) {
		delete p_vertex;
	}
	for (auto &p_edge : edges) {
		delete p_edge;
	}
}
void Graph::add_edge(Edge *p_edge) {
	(*vertices[p_edge->p_src->id]).add_edge(p_edge);
	edges.push_back(p_edge);
}
void Graph::add_edge(int edge_src, int edge_dst, Weight w) {
	add_edge(new Edge(vertices[edge_src], vertices[edge_dst], w));
}
//void Graph::copy_vertex(int id_original, int id_copy) {
//	vertices[id_copy]->add_edge(vertices[id_original]->edges);
//}

// Graph.h end----------------------------------------------
// ShortPath.h start----------------------------------------
#pragma once

#include "Graph.h"

std::vector<Weight> dijkstra(const Graph &g, const int id_start, const Weight INF);
std::vector<Weight> bellman_ford(const Graph &g, const int id_start, const Weight INF);
std::vector<vector<Weight>> warshall_floyd(const Graph &g, const Weight INF);

//----------------------------------------------------------
#include <queue>


vector<Weight> dijkstra(const Graph &g, const int id_start, const Weight INF) { // INFに気を付けよ
	using Pwi = pair<Weight, int>;

	const size_t n = g.vertices.size();
	vector<Weight> d(n, INF);
	vector<bool> is_used(n, false);
	priority_queue<Pwi, vector<Pwi>, greater<Pwi>> que;

	d[id_start] = 0;
	for (que.push(Pwi(0, id_start)); !que.empty(); ) {
		Pwi p = que.top(); que.pop();
		const int tmpid = p.second; // p.first >= d[tmpid]
		if (is_used[tmpid]) continue;
		is_used[tmpid] = true;
		//assert(p.first == d[tmpid]);
		for (const auto &p_nxte : g.vertices[tmpid]->edges) {
			const int nxtid = p_nxte->p_dst->id;
			Weight nxtd = d[tmpid] + p_nxte->weight;
			if (d[nxtid] > nxtd) {
				d[nxtid] = nxtd;
				que.push(Pwi(nxtd, nxtid));
			}
		}
	}
	return d;
}

vector<Weight> bellman_ford(const Graph &g, const int id_start, const Weight INF) {
	// INFに気を付けよ d_max < INF
	// 負の閉路からアクセスできる頂点にはd = -INF
	const size_t n = g.vertices.size();
	vector<Weight> d(n, INF);
	d[id_start] = 0;
	bool is_negative_cycle = false;
	for (size_t i = 1; i <= n; i++)
	{
		for (const auto &p_tmpe : g.edges) {
			const int id_src = p_tmpe->p_src->id;
			const int id_dst = p_tmpe->p_dst->id;
			if (d[id_src] != INF && d[id_dst] > d[id_src] + p_tmpe->weight) {
				d[id_dst] = d[id_src] + p_tmpe->weight;
				if (i == n) {
					d[id_dst] = -INF;
					is_negative_cycle = true;
				}
			}
		}
	}
	if (is_negative_cycle) {
		for (size_t i = 1; i < n; i++)
		{
			for (const auto &p_tmpe : g.edges) {
				const int id_src = p_tmpe->p_src->id;
				const int id_dst = p_tmpe->p_dst->id;
				if (d[id_src] == -INF && d[id_dst] != -INF) {
					d[id_dst] = -INF;
				}
			}
		}
	}
	return d;
}

vector<vector<Weight>> warshall_floyd(const Graph &g, const Weight INF) {
	const size_t n = g.vertices.size();
	vector<vector<Weight>> d(n, vector<Weight>(n, INF));
	for (size_t id = 0; id < n; id++)
	{
		d[id][id] = 0;
	}

	for (const auto &p_tmpe : g.edges) {
		d[p_tmpe->p_src->id][p_tmpe->p_dst->id] = p_tmpe->weight;
	}
	for (size_t id_new = 0; id_new < n; id_new++)
	{
		for (size_t id_src = 0; id_src < n; id_src++)
		{
			for (size_t id_dst = 0; id_dst < n; id_dst++)
			{
				if (d[id_src][id_dst] > d[id_src][id_new] + d[id_new][id_dst]) {
					d[id_src][id_dst] = d[id_src][id_new] + d[id_new][id_dst];
				}
			}
		}
	}
	return d;
}

// ShortPath.h end------------------------------------------

int N, M;
vector<int> A, B;

int main() {
	cin >> N >> M;
	for (int i = 0; i < N - 1 + M; i++) {
		int tmpa, tmpb;
		cin >> tmpa >> tmpb;
		A.push_back(tmpa);
		B.push_back(tmpb);
	}
	unordered_set<int> us;
	for (int i = 1; i <= N; i++)
	{
		us.insert(i);
	}
	for (const int b : B) {
		us.erase(b);
	}
	const int root = *us.begin();

	Graph g(N + 1);
	for (int i = 0; i < N - 1 + M; i++)
	{
		g.add_edge(A[i], B[i], -1);
	}
	vector<Weight> distance = bellman_ford(g, root, 9999);
	Graph originalg(N + 1);
	for (const auto e : g.edges)
	{
		if (distance[e->p_src->id] - distance[e->p_dst->id] == 1) {
			originalg.add_edge(e->p_dst->id, e->p_src->id, 0);
		}
	}
	for (int i = 1; i <= N; i++)
	{
		if (i == root) {
			cout << 0;
		}
		else {
			cout << (*(originalg.vertices[i]->edges.begin()))->p_dst->id;
		}
		cout << endl;
	}
	return 0;
}


Compiler output:
a.cc:28:9: warning: #pragma once in main file
   28 | #pragma once
      |         ^~~~
a.cc:119:9: warning: #pragma once in main file
  119 | #pragma once
      |         ^~~~
a.cc:121:10: fatal error: Graph.h: No such file or directory
  121 | #include "Graph.h"
      |          ^~~~~~~~~
compilation terminated.