Finding the longest cycle in a directed graph using DFS

I need to find the longest cycle in a directed graph using DFS.

I once saw this Wikipedia article describing the way of doing this, and I think it approached the problem something like marking the node with one of three states: Node not yet visited, Finished searching the node, and Node visited, but not yet finished visiting.

I would be grateful if anyone could share the link with me. By the way, it isn't Tarjan's Algorithm.

The problem below is what I'm trying to solve, in case you'd like to know.

The two digits given in the first line is N and M, each representing the number of nodes and the number of directed edges.

From the second line is given M sets of two digits A and B, which means that node A and B are connected but you can only traverse the node from A to B.

input.txt:

7 9  
1 2  
2 3  
3 1  
3 4  
4 5  
5 1  
5 6  
6 7  
7 2  

The answer in this case is 6, since 2>3>4>5>6>7>2.

15
задан Bill the Lizard 19 September 2012 в 22:01
поделиться