// DFS for directed graphs // BPW, 4/2003 #include #include #define WHITE 0 #define GRAY 1 #define BLACK 2 #define TREE 0 #define BACK 1 #define CROSS 2 #define FORWARD 3 int n; // number of nodes int e; // number of edges struct edge { int tail,head,type; }; typedef struct edge edgeType; edgeType *edgeTab; int *firstEdge; // Table indicating first in range of edges with a common tail int *discovery,*finish,*predecessor,*vertexStatus; // Reading the input file and organize adjacency lists int tailThenHead(const void* xin, const void* yin) // Used in calls to qsort() and bsearch() for read_input_file() { int result; edgeType *x,*y; x=(edgeType*) xin; y=(edgeType*) yin; result=x->tail - y->tail; if (result!=0) return result; else return x->head - y->head; } void read_input_file() { int a,b,i,j; edgeType work; edgeType *ptr; scanf("%d %d",&n,&e); edgeTab=(edgeType*) malloc(e*sizeof(edgeType)); if (!edgeTab) { printf("edgeTab malloc failed %d\n",__LINE__); exit(0); } for (i=0; i=n || b<0 || b>=n) { printf("Invalid input %d %d at %d\n",a,b,__LINE__); exit(0); } edgeTab[i].tail=a; edgeTab[i].head=b; } // sort edges qsort(edgeTab,e,sizeof(edgeType),tailThenHead); // Coalesce duplicates into a single edge j=0; for (i=1; i