Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


# 幅優先と深さ優先でトポロジカルソート
def set_indeg(u):
    # t = uとなる数を求める。
    ans = 0
    for line in G:
        if line[u] == 1:
            ans += 1
    return ans


def topologicalSortBfs(n):
    for i in range(n):
        color[i] = WHITE
        indeg[i] = set_indeg(i)

    for i in range(0, n):
        if indeg[i] == 0 and color[i] == WHITE:
            bfs(i)


def bfs(s):
    Q.appendleft(s)
    color[s] = GRAY
    while Q:
        u = Q.popleft()

        out.append(u)

        for v in range(n):
            if G[u][v] != 1:
                continue
            indeg[v] -= 1
            if indeg[v] == 0 and color[v] == WHITE:
                color[v] = GRAY
                Q.append(v)


def topologicalSortDfs(n):
    for i in range(n):
        color[i] = WHITE
        indeg[i] = set_indeg(i)

    for s in range(0, n):
        if color[s] == WHITE:
            dfs(s)


def dfs(u):
    color[u] = GRAY
    for v in range(n):
        if color[v] == WHITE and G[u][v] == 1:
            dfs(v)
    out.appendleft(u)


n, e = map(int, input().split())
G = [[0] * n for _ in range(n)]
for i in range(e):
    s, t = map(int, input().split())
    G[s][t] = 1
color = [None] * n
out = deque([])
indeg = [None] * n
WHITE = 0
GRAY = 1
Q = deque([])

topologicalSortDfs(n)
print(*out)
# 031452
# dfsがおかしい



Fixed submission:
from collections import deque


# 幅優先と深さ優先でトポロジカルソート


def topologicalSortBfs(n):
    for i in range(n):
        color[i] = WHITE

    for i in range(0, n):
        if indeg[i] == 0 and color[i] == WHITE:
            bfs(i)


def bfs(s):
    Q.appendleft(s)
    color[s] = GRAY
    while Q:
        u = Q.popleft()

        out.append(u)

        for v in G[u]:
            indeg[v] -= 1
            if indeg[v] == 0 and color[v] == WHITE:
                color[v] = GRAY
                Q.append(v)


def topologicalSortDfs(n):
    for i in range(n):
        color[i] = WHITE

    for s in range(0, n):
        if color[s] == WHITE:
            dfs(s)


def dfs(u):
    color[u] = GRAY
    for v in G[u]:
        if color[v] == WHITE:
            dfs(v)
    out.appendleft(u)


n, e = map(int, input().split())
G = {}
for i in range(n):
    G[i] = []
indeg = [0] * n
for i in range(e):
    s, t = map(int, input().split())
    indeg[t] += 1
    G[s].append(t)
color = [None] * n
out = deque([])
WHITE = 0
GRAY = 1
Q = deque([])

topologicalSortDfs(n)
print(*out)
# 031452
# dfsがおかしい

Buggy python submission (verdict: Runtime Error):
def topologicalSortBfs(n):
    for i in range(n):
        color[i] = WHITE

    for i in range(0, n):
        if indeg[i] == 0 and color[i] == WHITE:
            bfs(i)


def bfs(s):
    Q.appendleft(s)
    color[s] = GRAY
    while Q:
        u = Q.popleft()

        out.append(u)

        for v in G[u]:
            indeg[v] -= 1
            if indeg[v] == 0 and color[v] == WHITE:
                color[v] = GRAY
                Q.append(v)


def topologicalSortDfs(n):
    for i in range(n):
        color[i] = WHITE

    for s in range(0, n):
        if color[s] == WHITE:
            dfs(s)


def dfs(u):
    color[u] = GRAY
    for v in G[u]:
        if color[v] == WHITE:
            dfs(v)
    out.appendleft(u)


n, e = map(int, input().split())
G = {}
for i in range(n):
    G[i] = []
indeg = [0] * n
for i in range(e):
    s, t = map(int, input().split())
    indeg[t] += 1
    G[s].append(t)
color = [None] * n
out = deque([])
WHITE = 0
GRAY = 1
Q = deque([])

topologicalSortDfs(n)
print(*out)


Fixed submission:
from collections import deque


# 幅優先と深さ優先でトポロジカルソート


def topologicalSortBfs(n):
    for i in range(n):
        color[i] = WHITE

    for i in range(0, n):
        if indeg[i] == 0 and color[i] == WHITE:
            bfs(i)


def bfs(s):
    Q.appendleft(s)
    color[s] = GRAY
    while Q:
        u = Q.popleft()

        out.append(u)

        for v in G[u]:
            indeg[v] -= 1
            if indeg[v] == 0 and color[v] == WHITE:
                color[v] = GRAY
                Q.append(v)


def topologicalSortDfs(n):
    for i in range(n):
        color[i] = WHITE

    for s in range(0, n):
        if color[s] == WHITE:
            dfs(s)


def dfs(u):
    color[u] = GRAY
    for v in G[u]:
        if color[v] == WHITE:
            dfs(v)
    out.appendleft(u)


n, e = map(int, input().split())
G = {}
for i in range(n):
    G[i] = []
indeg = [0] * n
for i in range(e):
    s, t = map(int, input().split())
    indeg[t] += 1
    G[s].append(t)
color = [None] * n
out = deque([])
WHITE = 0
GRAY = 1
Q = deque([])

topologicalSortDfs(n)
print(*out)
# 031452
# dfsがおかしい