Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np


def is_input(node, path):
    for i in range(len(path)):
        if path[i][node]:
            return True
    return False


def topological_sort(path):
    S = []
    for i, input_nodes in enumerate(input_nodes_list):
        if len(input_nodes_list) == 0:
            S.append(i)
    path = path.copy()
    for i in range(len(path)):
        flag = True
        for j in range(len(path)):
            if path[j][i]:
                flag = False
                input_nodes_list[i].append(j)
        if flag:
            S.append(i)
    res = []
    while len(S) > 0:
        now = S.pop()
        res.append(now)
        for i, f in enumerate(path[now]):
            if f:
                path[now][i] = False
                if not is_input(i, path):
                    S.append(i)
    return res


N, M = map(int, input().split(" "))
path = np.array([[False for i in range(N)] for i in range(N)])
input_nodes_list = [[] for i in range(N)]

for i in range(N+M-1):
    a, b = map(int, input().split(" "))
    path[a-1][b-1] = True
    input_nodes_list[b-1].append(a-1)

sorted_nodes = topological_sort(path)
dict_sorted_nodes = {}
for i, v in enumerate(sorted_nodes):
    dict_sorted_nodes[v] = i


for input_nodes in input_nodes_list:
    if len(input_nodes) == 0:
        print(0)
        continue
    if len(input_nodes) == 1:
        print(input_nodes[0]+1)
        continue
    temp_i = 0
    for j in input_nodes:
        temp_i = max(temp_i, dict_sorted_nodes[j])

    print(sorted_nodes[temp_i]+1)



Fixed submission:
from collections import deque
from operator import itemgetter


def topolo():
    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    res = []
    popleft, append = q.popleft, q.append
    res_append = res.append
    while q:
        v1 = popleft()
        res_append(v1)
        for v2 in edges[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                append(v2)
    return res


N, M = map(int, input().split(" "))
edges = [[] for _ in [0]*N]
rev = [[] for _ in [0]*N]

ins = [0]*N

for a, b in (map(int, input().split(" ")) for _ in [0]*(N+M-1)):
    edges[a-1].append(b-1)
    rev[b-1].append(a-1)
    ins[b-1] += 1

sorted_nodes = topolo()
order = [i for i, v in sorted(enumerate(sorted_nodes), key=itemgetter(1))]

for input_nodes in rev:
    l = len(input_nodes)
    if l == 0:
        print(0)
        continue
    elif l == 1:
        print(input_nodes[0] + 1)
        continue
    temp = 0
    for node in input_nodes:
        temp = max(temp, order[node])
    print(sorted_nodes[temp] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np


def is_input(node, path):
    for i in range(len(path)):
        if path[i][node]:
            return True
    return False


def topological_sort(path):
    S = []
    path = path.copy()
    for i in range(len(path[0])):
        if any(path[:, i]):
            pass
        else:
            S.append(i)
    res = []
    while len(S) > 0:
        now = S.pop()
        res.append(now)
        for i, f in enumerate(path[now]):
            if f:
                path[now][i] = False
                if not is_input(i, path):
                    S.append(i)
    return res


N, M = map(int, input().split(" "))
path = np.array([[False for i in range(N)] for i in range(N)])


for i in range(N+M-1):
    a, b = map(int, input().split(" "))
    path[a-1][b-1] = True

sorted_nodes = topological_sort(path)
dict_sorted_nodes = {}
for i, v in enumerate(sorted_nodes):
    dict_sorted_nodes[v] = i


for line in path.T:
    input_nodes = []
    for j, f in enumerate(line):
        if f:
            input_nodes.append(j)

    if len(input_nodes) == 0:
        print(0)
        continue
    if len(input_nodes) == 1:
        print(input_nodes[0]+1)
        continue
    temp_i = 0
    for j in input_nodes:
        temp_i = max(temp_i, dict_sorted_nodes[j])

    print(sorted_nodes[temp_i]+1)



Fixed submission:
from collections import deque
from operator import itemgetter


def topolo():
    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    res = []
    popleft, append = q.popleft, q.append
    res_append = res.append
    while q:
        v1 = popleft()
        res_append(v1)
        for v2 in edges[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                append(v2)
    return res


N, M = map(int, input().split(" "))
edges = [[] for _ in [0]*N]
rev = [[] for _ in [0]*N]

ins = [0]*N

for a, b in (map(int, input().split(" ")) for _ in [0]*(N+M-1)):
    edges[a-1].append(b-1)
    rev[b-1].append(a-1)
    ins[b-1] += 1

sorted_nodes = topolo()
order = [i for i, v in sorted(enumerate(sorted_nodes), key=itemgetter(1))]

for input_nodes in rev:
    l = len(input_nodes)
    if l == 0:
        print(0)
        continue
    elif l == 1:
        print(input_nodes[0] + 1)
        continue
    temp = 0
    for node in input_nodes:
        temp = max(temp, order[node])
    print(sorted_nodes[temp] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import numpy as np
import bisect


def is_input(node, path):
    for i in range(len(path)):
        if path[i][node]:
            return True
    return False


def topological_sort(path):
    S = []
    path = np.array(path)
    for i in range(len(path[0])):
        if any(path[:, i]):
            pass
        else:
            S.append(i)
    res = []
    while len(S) > 0:
        now = S.pop()
        res.append(now)
        for i, f in enumerate(path[now]):
            if f:
                path[now][i] = False
                if not is_input(i, path):
                    S.append(i)
    return res

N, M = map(int, input().split(" "))
path = [[False for i in range(N)] for i in range(N)]


for i in range(N+M-1):
    a, b = map(int, input().split(" "))
    path[a-1][b-1] = True

sorted_nodes = topological_sort(path)

input_nodes_list = [[] for i in range(N)]
for i, line in enumerate(path):
    temp = []
    for j, f in enumerate(line):
        if f:
            input_nodes_list[j].append(i)
ans = []
dict_sorted_nodes = {}
for i, v in enumerate(sorted_nodes):
    dict_sorted_nodes[v] = i

for input_nodes in input_nodes_list:
    if len(input_nodes) == 0:
        print(0)
        continue
    if len(input_nodes) == 1:
        print(input_nodes[0]+1)
        continue
    temp_i = 0
    for i in input_nodes:
        temp_i = max(temp_i, dict_sorted_nodes[i])

    print(sorted_nodes[temp_i]+1)



Fixed submission:
from collections import deque
from operator import itemgetter


def topolo():
    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    res = []
    popleft, append = q.popleft, q.append
    res_append = res.append
    while q:
        v1 = popleft()
        res_append(v1)
        for v2 in edges[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                append(v2)
    return res


N, M = map(int, input().split(" "))
edges = [[] for _ in [0]*N]
rev = [[] for _ in [0]*N]

ins = [0]*N

for a, b in (map(int, input().split(" ")) for _ in [0]*(N+M-1)):
    edges[a-1].append(b-1)
    rev[b-1].append(a-1)
    ins[b-1] += 1

sorted_nodes = topolo()
order = [i for i, v in sorted(enumerate(sorted_nodes), key=itemgetter(1))]

for input_nodes in rev:
    l = len(input_nodes)
    if l == 0:
        print(0)
        continue
    elif l == 1:
        print(input_nodes[0] + 1)
        continue
    temp = 0
    for node in input_nodes:
        temp = max(temp, order[node])
    print(sorted_nodes[temp] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter


def get_root(A):
    res = [A]
    search = [A]
    visit = [False for i in range(N)]
    while(len(search) > 0):
        now = search.pop()
        if visit[now-1]:
            continue
        visit[now-1] = True
        for a in ans[now-1]:
            if a == 0:
                continue
            if visit[a-1]:
                continue
            res.append(a)
            search.append(a)
    return set(res)


N, M = map(int, input().split(" "))
AB = [list(map(int, input().split(" "))) for i in range(N+M-1)]

AB = sorted(AB, key=itemgetter(1))
count = 1
before = -1
temp = [AB[0][0]]
ans = [[0, ] for i in range(N)]
before = AB[0][1]

for a, b in AB[1:]:
    if before != b:
        ans[before-1] = temp
        temp = [a, ]
        before = b
    else:
        temp.append(a)

if len(temp) > 0:
    ans[-1] = (temp)

new_ans = []
for each_ans in ans:
    if len(each_ans) == 1:
        new_ans.append(each_ans[0])
        continue
    for a in each_ans:
        setans = set(each_ans)
        if len(setans & get_root(a)) == len(setans):
            new_ans.append(a)
            break

for line in new_ans:
    print(line)


Fixed submission:
from collections import deque
from operator import itemgetter


def topolo():
    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    res = []
    popleft, append = q.popleft, q.append
    res_append = res.append
    while q:
        v1 = popleft()
        res_append(v1)
        for v2 in edges[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                append(v2)
    return res


N, M = map(int, input().split(" "))
edges = [[] for _ in [0]*N]
rev = [[] for _ in [0]*N]

ins = [0]*N

for a, b in (map(int, input().split(" ")) for _ in [0]*(N+M-1)):
    edges[a-1].append(b-1)
    rev[b-1].append(a-1)
    ins[b-1] += 1

sorted_nodes = topolo()
order = [i for i, v in sorted(enumerate(sorted_nodes), key=itemgetter(1))]

for input_nodes in rev:
    l = len(input_nodes)
    if l == 0:
        print(0)
        continue
    elif l == 1:
        print(input_nodes[0] + 1)
        continue
    temp = 0
    for node in input_nodes:
        temp = max(temp, order[node])
    print(sorted_nodes[temp] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter


def get_root(A):
    res = [A]
    search = [A]
    visit = [False for i in range(N)]
    while(len(search) > 0):
        now = search.pop()
        if visit[now-1]:
            continue
        visit[now-1] = True
        for a in ans[now-1]:
            if a == 0:
                continue
            if visit[a-1]:
                continue
            res.append(a)
            search.append(a)
    return set(res)


N, M = map(int, input().split(" "))
AB = [list(map(int, input().split(" "))) for i in range(N+M-1)]

AB = sorted(AB, key=itemgetter(1))
count = 1
before = -1
temp = [AB[0][0]]
ans = [[0, ] for i in range(N)]
before = AB[0][1]
for a, b in AB[1:]:
    if before != b:
        ans[before-1] = temp
        temp = [a, ]
        before = b
    else:
        temp.append(a)

if len(temp) > 0:
    ans[-1] = (temp)

new_ans = []
for i, each_ans in enumerate(ans):
    if len(each_ans) == 1:
        new_ans.append(each_ans[0])
        continue
    for a in each_ans:
        if set(each_ans) & get_root(a) == set(each_ans):
            new_ans.append(a)
            break

for line in new_ans:
    print(line)


Fixed submission:
from collections import deque
from operator import itemgetter


def topolo():
    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    res = []
    popleft, append = q.popleft, q.append
    res_append = res.append
    while q:
        v1 = popleft()
        res_append(v1)
        for v2 in edges[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                append(v2)
    return res


N, M = map(int, input().split(" "))
edges = [[] for _ in [0]*N]
rev = [[] for _ in [0]*N]

ins = [0]*N

for a, b in (map(int, input().split(" ")) for _ in [0]*(N+M-1)):
    edges[a-1].append(b-1)
    rev[b-1].append(a-1)
    ins[b-1] += 1

sorted_nodes = topolo()
order = [i for i, v in sorted(enumerate(sorted_nodes), key=itemgetter(1))]

for input_nodes in rev:
    l = len(input_nodes)
    if l == 0:
        print(0)
        continue
    elif l == 1:
        print(input_nodes[0] + 1)
        continue
    temp = 0
    for node in input_nodes:
        temp = max(temp, order[node])
    print(sorted_nodes[temp] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter


def get_root(A):
    res = [A]
    search = [A]

    while(len(search) > 0):
        now = search.pop()
        for a in ans[now-1]:
            if a == 0:
                continue
            res.append(a)
            search.append(a)
    return set(res)


N, M = map(int, input().split(" "))
AB = [list(map(int, input().split(" "))) for i in range(N+M-1)]

AB = sorted(AB, key=itemgetter(1))
count = 1
before = -1
temp = [AB[0][0]]
ans = [[0, ] for i in range(N)]
before = AB[0][1]
for a, b in AB[1:]:
    if before != b:
        ans[before-1] = temp
        temp = [a, ]
        before = b
    else:
        temp.append(a)

if len(temp) > 0:
    ans[-1] = (temp)
new_ans = []
for i, each_ans in enumerate(ans):

    if len(each_ans) == 1:
        new_ans.append(each_ans[0])
        continue
    for a in each_ans:
        if set(each_ans) & get_root(a) == set(each_ans):
            new_ans.append(a)
            break

for line in new_ans:
    print(line)


Fixed submission:
from collections import deque
from operator import itemgetter


def topolo():
    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    res = []
    popleft, append = q.popleft, q.append
    res_append = res.append
    while q:
        v1 = popleft()
        res_append(v1)
        for v2 in edges[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                append(v2)
    return res


N, M = map(int, input().split(" "))
edges = [[] for _ in [0]*N]
rev = [[] for _ in [0]*N]

ins = [0]*N

for a, b in (map(int, input().split(" ")) for _ in [0]*(N+M-1)):
    edges[a-1].append(b-1)
    rev[b-1].append(a-1)
    ins[b-1] += 1

sorted_nodes = topolo()
order = [i for i, v in sorted(enumerate(sorted_nodes), key=itemgetter(1))]

for input_nodes in rev:
    l = len(input_nodes)
    if l == 0:
        print(0)
        continue
    elif l == 1:
        print(input_nodes[0] + 1)
        continue
    temp = 0
    for node in input_nodes:
        temp = max(temp, order[node])
    print(sorted_nodes[temp] + 1)