stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define putchar putchar_unlocked

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char ci[cm];

char *cn = ci + cm;

inline char getcha() {

	if (cn - ci == cm) { fread(ci, 1, cm, stdin); cn = ci; }

	return *cn++;

}



inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



inline void putint(int X) {

	if (X == 0) putchar('0');

	int keta = 0;

	char C[10];

	while (X) {

		C[keta] = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--) putchar(C[i]);

	putchar('\n');

}



int oya;

int D[100001];

int P[100001];

vector<int> V[100001];

int sagasu(int A) {

	if (*(D + A)) return *(D + A);

	if (A == oya) return 1;

	int ret = 0;

	for (int i : *(V + A)) {

		int kari = sagasu(i) + 1;

		if (ret < kari) {

			ret = kari;

			*(P + A) = i;

		}

	}

	*(D + A) = ret;

	return ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	N = getint();

	M = getint();

	bool kodomo[100001] = {};

	rep(i, N + M - 1) {

		int a, b;

		a = getint();

		b = getint();

		V[b].pb(a);

		kodomo[b] = true;

	}



	rep1(i, N) {

		if (!kodomo[i]) {

			oya = i;

			break;

		}

	}



	rep1(i, N) {

		sagasu(i);

		putint(P[i]);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.5888792675737693x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return 	D[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	rep1(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		co(P[i]);

	}



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	vector<int> IE[100001];

	int num = 1;

	int topo[100001], jisuu[100001] = {};

	rep(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		IE[b].pb(a);

		jisuu[a]++;

	}



	stack<int> ST;

	rep1(i, N) {

		if(!jisuu[i])ST.push(i);

	}

	while (ST.size()) {

		int k = ST.top();

		ST.pop();

		topo[k] = num++;

		for (int u : IE[k]) {

			jisuu[u]--;

			if (!jisuu[u]) ST.push(u);

		}

	}



	rep1(i, N) {

		int kotae = 0;

		int saishou = 1e9;

		if (topo[i] == N) kotae = 0;

		else {

			for (int u : IE[i]) {

				if (saishou > topo[u]) {

					saishou = topo[u];

					kotae = u;

				}

			}

		}

		co(kotae);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.949268692990029x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return 	D[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	rep1(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		co(P[i]);

	}



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	vector<int> IE[100001];

	int num = 1;

	int topo[100001], jisuu[100001] = {};

	rep(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		IE[b].pb(a);

		jisuu[a]++;

	}



	vector<int> V;

	rep1(i, N) {

		if(!jisuu[i]) V.pb(i);

	}

	while (V.size()) {

		int k = V.back();

		V.pop_back();

		topo[k] = num++;

		for (int u : IE[k]) {

			jisuu[u]--;

			if (!jisuu[u]) V.pb(u);

		}

	}



	rep1(i, N) {

		int kotae = 0;

		int saishou = 1e9;

		if (topo[i] == N) kotae = 0;

		else {

			for (int u : IE[i]) {

				if (saishou > topo[u]) {

					saishou = topo[u];

					kotae = u;

				}

			}

		}

		co(kotae);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.949431946395641x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return 	D[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	rep1(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		co(P[i]);

	}



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int oya;

int D[100001];

int P[100001];

vector<int> V[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	if (A == oya) return 1;

	int ret = 0;

	for (int i : V[A]) {

		int kari = sagasu(i) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = i;

		}

	}

	D[A] = ret;

	return ret;

}





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	int kodomo[100001] = {};

	rep(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		V[b].pb(a);

		kodomo[b] = 1;

	}



	rep1(i, N) {

		if (kodomo[i] == 0) {

			oya = i;

			break;

		}

	}



	rep1(i, N) {

		sagasu(i);

		co(P[i]);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 21.343952885153296x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return D[A] = ret;

}



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 20;

char dn[dm], * di = dn;

inline void putint(int X) {

	if (X == 0) {

		*di++ = '0';

		*di++ = '\n';

		return;

	}

	int keta = 0;

	char C[10];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	rep1(i, N + M - 1) {

		int a = getint(), b = getint();

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		putint(P[i]);

	}

	fwrite(dn, 1, di - dn, stdout);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	vector<int> IE[100001];

	int num = 1;

	int topo[100001], jisuu[100001] = {};

	rep(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		IE[b].pb(a);

		jisuu[a]++;

	}



	stack<int> ST;

	rep1(i, N) {

		if(!jisuu[i])ST.push(i);

	}

	while (ST.size()) {

		int k = ST.top();

		ST.pop();

		topo[k] = num++;

		for (int u : IE[k]) {

			jisuu[u]--;

			if (!jisuu[u]) ST.push(u);

		}

	}



	rep1(i, N) {

		int kotae = 0;

		int saishou = 1e9;

		if (topo[i] == N) kotae = 0;

		else {

			for (int u : IE[i]) {

				if (saishou > topo[u]) {

					saishou = topo[u];

					kotae = u;

				}

			}

		}

		co(kotae);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 21.566051289398338x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return D[A] = ret;

}



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 20;

char dn[dm], * di = dn;

inline void putint(int X) {

	if (X == 0) {

		*di++ = '0';

		*di++ = '\n';

		return;

	}

	int keta = 0;

	char C[10];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	rep1(i, N + M - 1) {

		int a = getint(), b = getint();

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		putint(P[i]);

	}

	fwrite(dn, 1, di - dn, stdout);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	vector<int> IE[100001];

	int num = 1;

	int topo[100001], jisuu[100001] = {};

	rep(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		IE[b].pb(a);

		jisuu[a]++;

	}



	vector<int> V;

	rep1(i, N) {

		if(!jisuu[i]) V.pb(i);

	}

	while (V.size()) {

		int k = V.back();

		V.pop_back();

		topo[k] = num++;

		for (int u : IE[k]) {

			jisuu[u]--;

			if (!jisuu[u]) V.pb(u);

		}

	}



	rep1(i, N) {

		int kotae = 0;

		int saishou = 1e9;

		if (topo[i] == N) kotae = 0;

		else {

			for (int u : IE[i]) {

				if (saishou > topo[u]) {

					saishou = topo[u];

					kotae = u;

				}

			}

		}

		co(kotae);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 21.566942778834054x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return D[A] = ret;

}



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 20;

char dn[dm], * di = dn;

inline void putint(int X) {

	if (X == 0) {

		*di++ = '0';

		*di++ = '\n';

		return;

	}

	int keta = 0;

	char C[10];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	rep1(i, N + M - 1) {

		int a = getint(), b = getint();

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		putint(P[i]);

	}

	fwrite(dn, 1, di - dn, stdout);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return 	D[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	rep1(i, N + M - 1) {

		int a, b;

		cin >> a >> b;

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		co(P[i]);

	}



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.4607708327565x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



//当時の自分の方が天才っぽい

int D[100001];

int P[100001];

int to[100001], ne[100001], he[100001];

int sagasu(int A) {

	if (D[A]) return D[A];

	int ret = 1;

	for (int i = he[A]; i; i = ne[i]) {

		int u = to[i];

		int kari = sagasu(u) + 1;

		if (ret < kari) {

			ret = kari;

			P[A] = u;

		}

	}

	return D[A] = ret;

}



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 20;

char dn[dm], * di = dn;

inline void putint(int X) {

	if (X == 0) {

		*di++ = '0';

		*di++ = '\n';

		return;

	}

	int keta = 0;

	char C[10];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	rep1(i, N + M - 1) {

		int a = getint(), b = getint();

		to[i] = a;

		ne[i] = he[b];

		he[b] = i;

	}



	rep1(i, N) {

		sagasu(i);

		putint(P[i]);

	}

	fwrite(dn, 1, di - dn, stdout);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main(){

  int n,m;cin >> n >> m;

  vector<int> g[100005];

  vector<int> h(n),ans(n);

  for(int i=0;i<n+m-1;i++){

    int a,b;cin >> a >> b;

    a--,b--;

    g[a].push_back(b);

    h[b]++;

  }

  int rt=-1;

  for(int i=0;i<n;i++){

    if(h[i]==0){

      rt=i;

    }

  }

  queue<int> q;

  q.push(rt);

  ans[rt]=0;

  while(!q.empty()){

    int v=q.front();

    q.pop();

    for(auto x:g[v]){

      h[x]--;

      if(h[x]==0){

        ans[x]=v+1;

        q.push(x);

      }

    }

  }

  for(auto x:ans){

    cout << x << endl;

  }

}

// Faster C++ solution for the same problem (measured speedup: 3.5067716026550166x):
#include<bits/stdc++.h>

using namespace std;

int main(){

    int n,m;cin >> n >> m;

    vector<vector<int>> g(n);

    vector<int> h(n),ans(n);

    for(int i=0;i<n+m-1;i++){

        int a,b;cin >> a >> b;

        a--,b--;

        g[a].push_back(b);

        h[b]++;

    }

    int r;

    for(int i=0;i<n;i++){

        if(!h[i]){

            r=i;

            break;

        }

    }

    queue<int> q;

    q.push(r);

    ans[r]=0;

    while(!q.empty()){

        int p=q.front();

        q.pop();

        for(int x:g[p]){

            h[x]--;

            if(!h[x]){

                ans[x]=p+1;

                q.push(x);

            }

        }

    }

    for(int x:ans){

        cout << x << endl;

    }

}
// Slower C++ solution:
#include <iostream>

#include <sstream>

#include <string>

#include <vector>

#include <stack>

#include <queue>

#include <set>

#include <map>

#include <numeric>

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cctype>

#include <cmath>

#include <cassert>

#include <cstdarg>

#include <sys/time.h>

#include <fstream>

//#include "cout.h"



using namespace std;



#define SZ(x) ((int)x.size())

#define MSET(x,a) memset(x, a, (int)sizeof(x))

#define PB push_back

#define VI vector < int >

#define PII pair < int, int >

#define LL long long

#define FOR(i,a,b) for (int i = (a); i < (b); i++)

#define REP(i,n) FOR(i,0,n)

#define ALL(v) (v).begin(), (v).end()

#define FIT(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)

#define OUT(A) cout << #A << " = "<< (A) << endl

#define OUT2(A, B) cout << "(" << #A << ", " << #B << ") = (" << (A) << ", "<< (B) << ")" << endl

template<class T> void chmin(T &t, T f) { if (t > f) t = f; }

template<class T> void chmax(T &t, T f) { if (t < f) t = f; }



#define present(c, e) ((c).find((e)) != (c).end())

#define cpresent(c, e) (find(ALL(c), (e)) != (c).end())



int n, m;

int r;

vector<int> e[100010];

vector<int> topo;

bool used[100010];

int parent[100010];



void init() {



}



void dfs(int i) {

  if (used[i]) return;

  used[i] = true;

  REP(j, SZ(e[i])) dfs(e[i][j]);

  topo.push_back(i);

}



void makeTopo() {

  topo.clear();

  REP(i, n) dfs(i);

  reverse(ALL(topo));

}



void input() {

  REP(i, 100010) {

    // e[i].clear();

    parent[i] = -1;

  }

  cin >> n >> m;

  REP(i, n+m-1) {

    int a, b;

    cin >> a >> b;

    a--;

    b--;

    e[a].push_back(b);

  }

  makeTopo();

  REP(i, n) {;

    REP(j, SZ(e[topo[i]])) {

      parent[e[topo[i]][j]] = topo[i];

    }

  }

  REP(i, n) cout << parent[i] + 1 << endl;

}



void solve() {



}



int main() {

	init();

	input();

	solve();

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1178064699004049x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

typedef pair<int,P> P1;

typedef pair<P,P> P2;

#define pu push

#define pb push_back

#define mp make_pair

#define eps 1e-7

#define INF 1000000000

#define mod 1000000007

#define fi first

#define sc second

#define rep(i,x) for(int i=0;i<x;i++)

#define repn(i,x) for(int i=1;i<=x;i++)

#define SORT(x) sort(x.begin(),x.end())

#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())

#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())

#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int n,m;

vector<int>edge[100005];

int in[100005];

queue<int>que;

int ans[100005];

int main(){

	scanf("%d%d",&n,&m);

	m += n-1;

	rep(i,m){

		int a,b; scanf("%d%d",&a,&b);

		edge[a].pb(b);

		in[b]++;

	}

	repn(i,n) if(in[i]==0) que.push(i);

	while(!que.empty()){

		int q = que.front(); que.pop();

		for(int i=0;i<edge[q].size();i++){

			int to = edge[q][i];

			in[to]--;

			if(in[to] == 0) {ans[to] = q; que.push(to);}

		}

	}

	repn(i,n) printf("%d\n",ans[i]);

}