Graph Theory37 sections · 1633 units
Open in Course

Flight Routes Check - Implementation

(C++ code)

Implementation:

if numSCCs == 1:
    print "YES"
else:
    print "NO"
    a = -1
    b = -1
    for v from 1 to n:
        if a == -1:
            a = v
        else if scc[v] != scc[a]:
            b = v
            break
    print a, b

Find the first two nodes with different SCC IDs. This guarantees they are in different components.

This runs in O(V+E)O(V + E) time and uses O(V)O(V) space.