Graph Theory37 sections · 1633 units
Open in Course

Giant Pizza - Implementation

Code walkthrough.

function giantPizza(n, m, wishes):
    graph = adjacency list of size 2*m

    for each person (a, b):
        add edge from negate(a) to b
        add edge from negate(b) to a

    sccs = findSCCs(graph)

    for i from 1 to m:
        if sccId[i] == sccId[negate(i)]:
            return "IMPOSSIBLE"

    result = []
    for i from 1 to m:
        if sccId[i] > sccId[negate(i)]:
            result.append("+")
        else:
            result.append("-")

    return result

Time: O(V+E)O(V + E) for SCC finding. Space: O(V)O(V) for the data structures.