def dfs_topological_sort(arr, n):
    """ Topological sort with DFS. Return an empty list if
    there is a cycle.
    """
    graph = [[] for _ in range(n)]
    for u, v in arr:
        graph[u].append(v)

    visited, stack = [0] * n, []

    def dfs(u):
        if visited[u] == -1:
            return False
        if visited[u] == 1:
            return True

        visited[u] = -1
        for v in graph[u]:
            if not dfs(v):
                return -1
        stack.append(u)
        visited[u] = 1
        return True

    for u in range(n):
        if not dfs(u):
            return []
    return stack[::-1]