def reverse(graph):
    """replace all arcs (u, v) by arcs (v, u) in a graph"""
    rev_graph = [[] for node in graph]
    for node, _ in enumerate(graph):
        for neighbor in graph[node]:
            rev_graph[neighbor].append(node)
    return rev_graph