Here is the SCC-based implementation:
function flightRoutesCheck(n, adj):
sccId = kosarajuSCC(adj)
numSCCs = max(sccId) + 1
if numSCCs == 1:
print "YES"
return
// Find nodes in different SCCs
a = any node with sccId[a] == 0
b = any node with sccId[b] == 1
// Check reachability with BFS
canReach = BFS(adj, a, b)
print "NO"
if canReach:
print b, a
else:
print a, b
Time: . Space: .