stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
//#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
//#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
//#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
//#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

  }

}

// Optimized solution to the same problem (faster):
#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;

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



#define mx 100005



using namespace std;



vector<int> pa[mx], ch[mx];

stack<int> st;

int th[mx], n, m, a, b, rt, nm[mx], j;



int main(){

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

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

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

		pa[b].push_back(a);

		ch[a].push_back(b);

		nm[b]++;

	}

	for(rt = 1; !pa[rt].empty(); rt++);

	th[rt] = ++j;

	st.push(rt);

	while(!st.empty()){

		int i = st.top(); st.pop();

		for(int it : ch[i]){

			nm[it]--;

			if(!nm[it]){

				th[it] = ++j;

				st.push(it);

			}

		}

	}

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

		int ans = 0;

		for(int it : pa[i]) if(th[ans] < th[it]) ans = it;

		printf("%d\n", ans);

	}

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>



#define mx 100005



using namespace std;



int n, m, a, b, rt, pa[mx];

vector<int> ch[mx];

bool nt[mx], anc[mx] = {true};



 void search(int now){

	 anc[now] = true;

	 for(int i : ch[now]){

		 if(anc[pa[i]]) pa[i] = now;

		 if(nt[i]){

			 nt[i] = false;

			 search(i);

		 }

	 }

	 anc[now] = false;

 }



int main(){

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

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

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

		ch[a].push_back(b);

		nt[b] = true;

	}

	for(rt = 1; nt[rt]; rt++);

	search(rt);

	for(int i = 1; i <= n; i++) printf("%d\n", pa[i]);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int n, m, r;

vector<int> t1[114514], t2[114514], t3(114514);



void bfs(int s) {

	queue<pair<int, int> > q;

	vector<int> d(114514, -1), v(114514, 0);

	q.push(make_pair(s, 0));

	

	t3[s] = -1;

	d[s] = 0;

	

	while (!q.empty()) {

		pair<int, int> p = q.front(); q.pop();

		

		for (int i = 0; i < t2[p.first].size(); i++) {

			bool flag = true;

			

			v[t2[p.first][i]]++;

			

			/*for (int j = 0; j < t1[t2[p.first][i]].size(); j++) {

				//cout << t2[p.first][i] << ':' << t1[t2[p.first][i]][j] << ':' << d[t1[t2[p.first][i]][j]] << ':' << endl;

				if (t1[t2[p.first][i]][j] != p.first && d[t1[t2[p.first][i]][j]] == -1) {

					flag = false;

					break;

				}

			}*/

			

			if (v[t2[p.first][i]] != t1[t2[p.first][i]].size()) continue;

			

			//cout << p.first << ' ' << t2[p.first][i] << endl;

			

			d[t2[p.first][i]] = p.second + 1;

			t3[t2[p.first][i]] = p.first;

			q.push(make_pair(t2[p.first][i], p.second + 1));

		}

	}

}



int main(void) {

	cin >> n >> m;

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

		int a, b;

		cin >> a >> b;

		a--; b--;

		t1[b].push_back(a);

		t2[a].push_back(b);

	}

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

		if (t1[i].size() == 0) r = i;

	}

	bfs(r);

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

		cout << t3[i] + 1<< endl;

	}

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



int main(void) {

	int n, m, r;

	cin >> n >> m;

	vector<int> tree(n);

	vector<vector<int> > parent(n, vector<int>(0)), child(n, vector<int>(0));

	

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

		int a, b;

		cin >> a >> b;

		a--; b--;

		parent[b].push_back(a);

		child[a].push_back(b);

	}

	

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

		if (parent[i].size() == 0) r = i;

	}

	

	queue<int> q;

	vector<int> v(n, 0);

	q.push(r);

	

	tree[r] = -1;

	

	while (!q.empty()) {

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

		

		for (int i = 0; i < child[now].size(); i++) {

			bool flag = true;

			int next = child[now][i];

			

			v[next]++;

			

			if (v[next] != parent[next].size()) continue;

			

			tree[next] = now;

			q.push(next);

		}

	}

	

	for (int i = 0; i < n; i++) cout << tree[i] + 1 << endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<queue>

using namespace std; // 575



const int MAX = 100000;

struct Node {

	vector<int> edge, rev;

	int parent;

};



int main() {

	int N, M, A, B;

	cin >> N >> M;

	static Node node[MAX];

	for (int i = 0;i < N;++ i) node[i].parent = -1;

	for (int i = 0;i < N + M - 1;++ i) {

		cin >> A >> B;

		node[A - 1].edge.push_back(B - 1);

		node[B - 1].rev.push_back(A - 1);

	}

	int search;

	for (int i = 0;i < N;++ i) if (node[i].rev.size() == 0) search = i;

	queue<pair<int, int> > que;

	que.push(make_pair(search, 0));

	while(!que.empty()) {

		search = que.front().first;

		node[search].parent = que.front().second;

		que.pop();

		if (node[search].edge.empty()) continue;

		for (int j = 0;j < node[search].edge.size();++ j) {

			for (int k = 0;k < node[node[search].edge[j]].rev.size();++ k) {

				if (node[node[node[search].edge[j]].rev[k]].parent < 0) goto cont;

			}

			que.push(make_pair(node[search].edge[j], search + 1));

			cont:;

		}

	}

	for (int i = 0;i < N;++ i) cout << node[i].parent << endl;

	return 0;

}

/*

31536000のコメント解説欄

ここテンプレで用意してるから、A問題とかだとこの先空欄の危険あり

また、コンテスト後に https://31536000.hatenablog.com/ で解説していると思うので、良かったら読んでねー



まず、必ず増えた辺は子孫に向かってる→ショートカットである

よって、とりあえず根はO(N)で確定、どれからも辺が来てないはず

すると次の辺はその根からのみ矢印が来てるのでやっぱ確定

以下同様



各頂点について

1. 繋がってる全ての辺を調べる O(N+M)

2. その辺は、確定親以外から辺が来てないか調べる O(N+M)

はい

*/

// Optimized solution to the same problem (faster):
#include<iostream>

#include<vector>

#include<queue>

using namespace std; // 575

const int MAX = 100000;



int main() {

	int N, M, A, B;

	cin >> N >> M;

	static pair<int, vector<int>> node[MAX]; // 向かってくる辺の数、向かう辺の行き先

	for (int i = 0;i < N;++ i) node[i].first = 0;

	for (int i = 0;i < N + M - 1;++ i) {

		cin >> A >> B;

		node[A - 1].second.push_back(B - 1);

		++ node[B - 1].first;

	}

    static int search, ans[MAX];

	for (int i = 0;i < N;++ i) if (node[i].first == 0) search = i;

	queue<pair<int, int> > que; // 確定頂点とその親

	que.push(make_pair(search, 0));

	while(!que.empty()) {

		search = que.front().first;

		ans[search] = que.front().second;

		que.pop();

		for (int i = 0;i < node[search].second.size();++ i) if (-- node[node[search].second[i]].first <= 0) que.push(make_pair(node[search].second[i], search + 1));

	}

	for (int i = 0;i < N;++ i) cout << ans[i] << endl;

	return 0;

}

/*

31536000のコメント解説欄

ここテンプレで用意してるから、A問題とかだとこの先空欄の危険あり

また、コンテスト後に https://31536000.hatenablog.com/ で解説していると思うので、良かったら読んでねー



まず、必ず増えた辺は子孫に向かってる→ショートカットである

よって、とりあえず根はO(N)で確定、どれからも辺が来てないはず

すると次の辺はその根からのみ矢印が来てるのでやっぱ確定

以下同様



各頂点について

1. 繋がってる全ての辺を調べる O(N+M)

2. その辺は、確定親以外から辺が来てないか調べる O(N+M)

はい

*/

// Fastest known solution for this problem:
// Original C++ solution (slower):
/***********************

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

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

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

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,x,y,in[100010],num[100010],ans[100010];

vector <int> g[100010],dg[100010],topo;

bool vis[100010];



void dfs(int pos)

{

	vis[pos]=true;

	rep(i,g[pos].size())

	{

		if(vis[g[pos][i]]) continue;

		dfs(g[pos][i]);

	}

	topo.pb(pos);

}



int main()

{

	cin>>n>>m;

	rep(i,m+n-1)

	{

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		dg[y].pb(x);

		in[y]++;

	}

	repn(i,n)

	{

		if(in[i]==0)

		{

			dfs(i);

			break;

		}

	}

	reverse(topo.begin(),topo.end());

	rep(i,n) num[topo[i]]=i;

	rep(i,n)

	{

		if(in[topo[i]]==0) continue;

		int opt=-1,par;

		rep(j,dg[topo[i]].size()) 

		{

			if(num[dg[topo[i]][j]]>opt)

			{

				opt=num[dg[topo[i]][j]];

				par=dg[topo[i]][j];

			}

		}

		ans[topo[i]]=par;

	}

	repn(i,n) cout<<ans[i]<<endl;

	rt0;

}

/*

6 3

2 1

2 3

4 1

4 2

6 1

2 6

4 6

6 5

*/

// Optimized solution to the same problem (faster):
/***********************

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

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

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

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,x,y,in[100010],dist[100010],ans[100010];

vector <int> g[100010];



int dfs_init(int pos)

{

	//cout<<pos<<endl;

	if(dist[pos]!=-1) return dist[pos];//作为vis数组 

	rep(i,g[pos].size()) dist[pos]=max(dist[pos],dfs_init(g[pos][i])+1);

	if(dist[pos]==-1) dist[pos]=0;

	return dist[pos];

}



bool cmp(pii a,pii b)

{

	return a.fi>b.fi;

}



void dfs(int pos,int lst)

{

	if(ans[pos]!=-1) return;

	ans[pos]=lst;

	vector <pii> v;

	rep(i,g[pos].size()) v.pb({dist[g[pos][i]],g[pos][i]});

	sort(v.begin(),v.end(),cmp);

	rep(i,v.size()) dfs(v[i].se,pos);

}



int main()

{

	memset(dist,-1,sizeof(dist));

	memset(ans,-1,sizeof(ans));

	cin>>n>>m;

	rep(i,n+m-1)

	{

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		in[y]++;

	}

	repn(i,n)

	{

		if(in[i]==0)

		{

			dfs_init(i);

			dfs(i,0);

			repn(j,n) cout<<ans[j]<<endl;

			rt0;

		}

	}

	rt0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

	int a,b;

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

	int maxx=max(a+b,a-b);

	maxx=max(maxx,a*b);

	printf("%d\n",maxx);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>





int main(){

	int a,b;

	

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

	

	int tambah=a+b;

	int kurang=a-b;

	int kali=a*b;

	

	printf("%d\n", tambah>=kurang&&tambah>=kali?tambah:(kurang>=kali&&kurang>=tambah?kurang:kali));

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;

int main() {

    int a,b;

    cin>>a>>b;

    int x=a+b;

    int y=a-b;

    int z=a*b;

    if(x>=y&&x>=z)cout<<x;

    else if(x<=y&&y>=z) cout<<y;

    else if(z>=x&&z>=y) cout<<z;



    }


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>





int main(){

	int a,b;

	

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

	

	int tambah=a+b;

	int kurang=a-b;

	int kali=a*b;

	

	printf("%d\n", tambah>=kurang&&tambah>=kali?tambah:(kurang>=kali&&kurang>=tambah?kurang:kali));

	

	return 0;

}

// Fastest known solution for this problem: