Language: C++

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;
//using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
    void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }




    void solve()
    {
        int N = cin.nextint;
        int M = cin.nextint;
        var G = Enumerable.Range(0, N).Select(i => new List<int>()).ToArray();
        //H = Enumerable.Range(0, N).Select(i => new List<int>()).ToArray();


        var P = new int[N];
        var F = new int[N];
        var S = new int[N];
        for (int i = 0; i < N; i++)
        {
            P[i] = -100;
            F[i] = -100;
        }

        for (int i = 0; i < N + M - 1; i++)
        {
            int a = cin.nextint - 1;
            int b = cin.nextint - 1;
            G[a].Add(b);
            S[b]++;
        }

        for (int i = 0; i < N; i++)
        {
            if (S[i] == 0)
            {
                P[i] = -1;
                F[i] = 0;

                var A = new Queue<int>();
                A.Enqueue(i);
                var B = new Queue<int>();
                B.Enqueue(-1);
                var C = new Queue<int>();
                C.Enqueue(0);

                while (A.Any())
                {
                    var u = A.Dequeue();
                    var p = B.Dequeue();
                    var d = C.Dequeue();

                    if (F[u] != d)
                    {
                        continue;
                    }

                    foreach (var v in G[u])
                    {
                        if (S[v] == 1)
                        {
                            P[v] = u;
                            F[v] = F[u] + 1;

                            A.Enqueue(v);
                            B.Enqueue(u);
                            C.Enqueue(F[v]);
                        }
                        else
                        {
                            S[v]--;
                        }
     

                    }
                }

                break;
            }
        }

        for (int i = 0; i < N; i++)
        {
            WriteLine(P[i] + 1);
        }
    }

}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
    public void join<T>(IEnumerable<T> values) { WriteLine(string.Join(" ", values)); }
}


Compiler output:
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Linq;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'System'
    4 | using System.IO;
      |       ^~~~~~
a.cc:9:7: error: expected nested-name-specifier before 'static'
    9 | using static System.Console;
      |       ^~~~~~
a.cc:22:5: error: 'readonly' does not name a type
   22 |     readonly Scanner cin = new Scanner();
      |     ^~~~~~~~
a.cc:23:5: error: 'readonly' does not name a type
   23 |     readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
      |     ^~~~~~~~
a.cc:24:5: error: 'readonly' does not name a type
   24 |     readonly int mod = 1000000007;
      |     ^~~~~~~~
a.cc:25:10: error: variable or field 'chmax' declared void
   25 |     void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
      |          ^~~~~
a.cc:25:10: error: expected ';' at end of member declaration
   25 |     void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
      |          ^~~~~
      |               ;
a.cc:25:15: error: expected unqualified-id before '<' token
   25 |     void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
      |               ^
a.cc:26:10: error: variable or field 'chmin' declared void
   26 |     void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }
      |          ^~~~~
a.cc:26:10: error: expected ';' at end of member declaration
   26 |     void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }
      |          ^~~~~
      |               ;
a.cc:26:15: error: expected unqualified-id before '<' token
   26 |     void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }
      |               ^
a.cc:111:2: error: expected ';' after class definition
  111 | }
      |  ^
      |  ;
a.cc: In static member function 'static void Program::Main()':
a.cc:18:20: error: expected type-specifier before 'StreamWriter'
   18 |         SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
      |                    ^~~~~~~~~~~~
a.cc:18:9: error: 'SetOut' was not declared in this scope
   18 |         SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
      |         ^~~~~~
a.cc:19:22: error: expected ';' before '.' token
   19 |         new Program().solve();
      |                      ^
a.cc:20:9: error: 'Out' was not declared in this scope
   20 |         Out.Flush();
      |         ^~~
a.cc: In member function 'void Program::solve()':
a.cc:33:17: error: 'cin' was not declared in this scope
   33 |         int N = cin.nextint;
      |                 ^~~
a.cc:35:9: error: 'var' was not declared in this scope
   35 |         var G = Enumerable.Range(0, N).Select(i => new List<int>()).ToArray();
      |         ^~~
a.cc:39:13: error: expected ';' before 'P'
   39 |         var P = new int[N];
      |             ^
a.cc:40:13: error: expected ';' before 'F'
   40 |         var F = new int[N];
      |             ^
a.cc:41:13: error: expected ';' before 'S'
   41 |         var S = new int[N];
      |             ^
a.cc:44:13: error: 'P' was not declared in this scope
   44 |             P[i] = -100;
      |             ^
a.cc:45:13: error: 'F' was not declared in this scope
   45 |             F[i] = -100;
      |             ^
a.cc:52:13: error: 'G' was not declared in this scope
   52 |             G[a].Add(b);
      |             ^
a.cc:53:13: error: 'S' was not declared in this scope
   53 |             S[b]++;
      |             ^
a.cc:58:17: error: 'S' was not declared in this scope
   58 |             if (S[i] == 0)
      |                 ^
a.cc:60:17: error: 'P' was not declared in this scope
   60 |                 P[i] = -1;
      |                 ^
a.cc:61:17: error: 'F' was not declared in this scope
   61 |                 F[i] = 0;
      |                 ^
a.cc:63:21: error: expected ';' before 'A'
   63 |                 var A = new Queue<int>();
      |                     ^
a.cc:64:17: error: 'A' was not declared in this scope
   64 |                 A.Enqueue(i);
      |                 ^
a.cc:65:21: error: expected ';' before 'B'
   65 |                 var B = new Queue<int>();
      |                     ^
a.cc:66:17: error: 'B' was not declared in this scope
   66 |                 B.Enqueue(-1);
      |                 ^
a.cc:67:21: error: expected ';' before 'C'
   67 |                 var C = new Queue<int>();
      |                     ^
a.cc:68:17: error: 'C' was not declared in this scope
   68 |                 C.Enqueue(0);
      |                 ^
a.cc:72:25: error: expected ';' before 'u'
   72 |                     var u = A.Dequeue();
      |                         ^
a.cc:73:25: error: expected ';' before 'p'
   73 |                     var p = B.Dequeue();
      |                         ^
a.cc:74:25: error: expected ';' before 'd'
   74 |                     var d = C.Dequeue();
      |                         ^
a.cc:76:27: error: 'u' was not declared in this scope
   76 |                     if (F[u] != d)
      |                           ^
a.cc:76:33: error: 'd' was not declared in this scope
   76 |                     if (F[u] != d)
      |                                 ^
a.cc:81:34: error: expected ')' before 'v'
   81 |                     foreach (var v in G[u])
      |                                  ^
a.cc:81:29: note: to match this '('
   81 |                     foreach (var v in G[u])
      |                             ^
a.cc:81:21: error: 'foreach' was not declared in this scope
   81 |                     foreach (var v in G[u])
      |                     ^~~~~~~
a.cc:107:23: error: 'P' was not declared in this scope
  107 |             WriteLine(P[i] + 1);
      |                       ^
a.cc:107:13: error: 'WriteLine' was not declared in this scope
  107 |             WriteLine(P[i] + 1);
      |             ^~~~~~~~~
a.cc: At global scope:
a.cc:115:5: error: 'string' does not name a type
  115 |     string[] s; int i;
      |     ^~~~~~
a.cc:116:5: error: 'readonly' does not name a type
  116 |     readonly char[] cs = new char[] { ' ' };
      |     ^~~~~~~~
a.cc:117:11: error: expected ':' before 'Scanner'
  117 |     public Scanner() { s = new string[0]; i = 0; }
      |           ^~~~~~~~
      |           :
a.cc:118:11: error: expected ':' before 'string'
  118 |     public string[] scan { get { return ReadLine().Split(); } }
      |           ^~~~~~~
      |           :
a.cc:118:12: error: 'string' does not name a type
  118 |     public string[] scan { get { return ReadLine().Split(); } }
      |            ^~~~~~
a.cc:119:11: error: expected ':' before 'int'
  119 |     public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
      |           ^~~~
      |           :
a.cc:119:15: error: expected unqualified-id before '[' token
  119 |     public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
      |               ^
a.cc:120:11: error: expected ':' before 'long'
  120 |     public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
      |           ^~~~~
      |           :
a.cc:120:16: error: expected unqualified-id before '[' token
  120 |     public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
      |                ^
a.cc:121:11: error: expected ':' before 'double'
  121 |     public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
      |           ^~~~~~~
      |           :
a.cc:121:18: error: expected unqualified-id before '[' token
  121 |     public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
      |                  ^
a.cc:122:11: error: expected ':' before 'string'
  122 |     public string next
      |           ^~~~~~~
      |           :
a.cc:122:12: error: 'string' does not name a type
  122 |     public string next
      |            ^~~~~~
a.cc:134:11: error: expected ':' before 'int'
  134 |     public int nextint { get { return int.Parse(next); } }
      |           ^~~~
      |           :
a.cc:134:58: error: expected ';' at end of member declaration
  134 |     public int nextint { get { return int.Parse(next); } }
      |                                                          ^
      |                                                           ;
a.cc:135:11: error: expected ':' before 'long'
  135 |     public long nextlong { get { return long.Parse(next); } }
      |           ^~~~~
      |           :
a.cc:135:61: error: expected ';' at end of member declaration
  135 |     public long nextlong { get { return long.Parse(next); } }
      |                                                             ^
      |                                                              ;
a.cc:136:11: error: expected ':' before 'double'
  136 |     public double nextdouble { get { return double.Parse(next); } }
      |           ^~~~~~~
      |           :
a.cc:136:67: error: expected ';' at end of member declaration
  136 |     public double nextdouble { get { return double.Parse(next); } }
      |                                                                   ^
      |                                                                    ;
a.cc:137:11: error: expected ':' before 'void'
  137 |     public void join<T>(IEnumerable<T> values) { WriteLine(string.Join(" ", values)); }
      |           ^~~~~
      |           :
a.cc:137:17: error: variable or field 'join' declared void
  137 |     public void join<T>(IEnumerable<T> values) { WriteLine(string.Join(" ", values)); }
      |                 ^~~~
a.cc:137:17: error: expected ';' at end of member declaration
  137 |     public void join<T>(IEnumerable<T> values) { WriteLine(string.Join(" ", values)); }
      |                 ^~~~
      |                     ;
a.cc:137:21