In a directed graph, list every vertex from which all other vertices are reachable by following arrows forward.
Medium4GraphDFSImplementationInterviewNo attempts yetTime limit2sMemory limit1024 MBA network of hyperspace highways runs through the galaxy. Each highway is a one way corridor between two planets. The galactic government wants to choose a planet for a police station.
For the police to protect the whole galaxy, it must be possible to travel from the police station to every planet over the highway network.
The police do not need a highway route back. On the way back they are in no hurry, so they may take routes slower than a highway.
You are given the network of one way highways. Find every planet from which all other planets can be reached by some sequence of highways.
The first line contains two integers N and M: the number of planets and the number of highways.
Each of the next M lines contains two integers Ai and Bi, the numbers of the planets joined by the i-th highway. That highway is one way and can only be used to travel from planet Ai to planet Bi.
Print two lines. The first line holds the number of planets that are suitable for the police station. The second line holds the numbers of those planets in ascending order, separated by single spaces. If no planet is suitable, print an empty second line.