Solving a graph theory problem using C