#include #include #include #include using namespace std; #define DBG #ifdef DBG #define PDBG(x); {cout< > AdjMat; set Visited; // Contains all the nodes that have been visited so far map NodeIndex; // Maps each string to a unique integer, an ID AdjMat Graph; // Maps each NodeIndex to a set of nodes that are connected to it int NodeLen, // The no. of nodes in the Graph root, // The Root of the current DFS steps, // The length of the path taken so far maxVal; // The max length Cycle so far /* Pre : Parent is a node in a graph that has an edge leading to child * Parent is a node in the visited set * Post : Returns true if and only if - * There is no loop between child and parent * parent is the only node in Visited that loves child * child doesn't love any one in the Visited set */ inline bool isTrueLove (int child, int parent) { set::iterator iter; if(Graph[child].find(parent) != Graph[child].end()) { // This means that the parent loves the child and the child loves the parent! PDBG("Child :"<::iterator child; PDBG("parent :"<>n; // Numbering of nodes starts from 1 //as operator [] return default value(which is 0 for int) when the key is not present NodeLen = 1; for(int i=0; i>lhs>>rhs; if( (li = NodeIndex[lhs]) == 0) li = NodeIndex[lhs] = NodeLen++; if( (ri = NodeIndex[rhs]) == 0) ri = NodeIndex[rhs] = NodeLen++; Graph[li].insert(ri); } #ifdef DBG map::iterator iter; for(int i=1; i::iterator iter = Graph[i].begin(); iter != Graph[i].end(); iter++) cout<<*iter<<" "; cout<2) cout<