4-1 Strongly Connected Components (17分)

Write a program to find the strongly connected components in a digraph.

Format of functions:

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

where Graph is defined as the following:

typedef struct VNode *PtrToVNode;
struct VNode {
    Vertex Vert;
    PtrToVNode Next;
};
typedef struct GNode *Graph;
struct GNode {
    int NumOfVertices;
    int NumOfEdges;
    PtrToVNode *Array;
};

Here void (*visit)(Vertex V) is a function parameter that is passed into StronglyConnectedComponents to handle (print with a certain format) each vertex that is visited. The function StronglyConnectedComponents is supposed to print a return after each component is found.

Sample program of judge:

#include 
#include 

#define MaxVertices 10  /* maximum number of vertices */
typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */
typedef struct VNode *PtrToVNode;
struct VNode {
    Vertex Vert;
    PtrToVNode Next;
};
typedef struct GNode *Graph;
struct GNode {
    int NumOfVertices;
    int NumOfEdges;
    PtrToVNode *Array;
};

Graph ReadG(); /* details omitted */

void PrintV( Vertex V )
{
   printf("%d ", V);
}

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

int main()
{
    Graph G = ReadG();
    StronglyConnectedComponents( G, PrintV );
    return 0;
}

/* Your function will be put here */

Sample Input (for the graph shown in the figure):

4 5
0 1
1 2
2 0
3 1
3 2

Sample Output:

3 
1 2 0

Note: The output order does not matter. That is, a solution like

0 1 2 
3

is also considered correct.

#include 
#include 

#define MaxVertices 10  /* maximum number of vertices */
typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */
typedef struct VNode *PtrToVNode;
struct VNode {
    Vertex Vert;
    PtrToVNode Next;
};
typedef struct GNode *Graph;
struct GNode {
    int NumOfVertices;
    int NumOfEdges;
    PtrToVNode *Array;
};

Graph ReadG(); /* details omitted */

void PrintV( Vertex V )
{
    printf("%d ", V);
}

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

int main()
{
    Graph G = ReadG();
    StronglyConnectedComponents( G, PrintV );
    return 0;
}

/* Your function will be put here */
PtrToVNode insert(PtrToVNode p,int x)  
{  
    p->Next=(PtrToVNode)malloc(sizeof(struct VNode));  
    p=p->Next;  
    p->Vert=x;  
    p->Next=NULL;  
    return p;  
}  

Graph ReadG()
{
	int i,j,m,n,x,y;  
    scanf("%d%d",&m,&n);   
    Graph p=(Graph)malloc(sizeof(struct GNode));  
    PtrToVNode *rear=(PtrToVNode*)malloc(m*sizeof(struct VNode));  
    p->Array=(PtrToVNode*)malloc(m*sizeof(struct VNode));  
    p->NumOfVertices=m;  
    p->NumOfEdges=n;  
    for(i=0;iArray[i]=(PtrToVNode)malloc(sizeof(struct VNode));  
        p->Array[i]->Next=NULL;  
    }  
    for(i=0;iArray[i];  
        p->Array[i]=p->Array[i]->Next;  
        tmp->Next=NULL;  
        free(tmp);  
    }   
    return p; 
}

int index=0;
int top=0;
int Stack[MaxVertices];
int DFN[MaxVertices];
int LOW[MaxVertices];
int vi[MaxVertices];
int whether[MaxVertices];

int min(int x, int y)
{
	if( x >= y )
	{
		return y;
	}
	else if( x < y )
	{
		return x;
	}
}

void Tarjan(int u, Graph G)
{
    int t, v;
    DFN[u]=LOW[u]=index++;
    Stack[top++]=u;
    vi[u]=1;
    struct VNode *temp;
    temp=G->Array[u];
    while(temp!=NULL)
    {
    	v=temp->Vert;
    	if(DFN[v]==-1)
    	{
    		Tarjan(v, G);
    		LOW[u]=min(LOW[u],LOW[v]);
		}
		else if(whether[v]==0)
		{
			LOW[u]=min(LOW[u],DFN[v]);
		}
		temp=temp->Next;
	}
	if(DFN[u]==LOW[u])
	{ 
	    do
	    {
	    	t=Stack[--top];
		    printf("%d ", t); 
		    whether[t]=1;
	    }while(u!=t);
	    printf("\n");
    }
}

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) )
{
	for(int i=0; iNumOfVertices; i++)
	{
		vi[i]=0;
		whether[i]=0;
		DFN[i]=LOW[i]=-1;
	}
	for(int i=0; iNumOfVertices; i++)
	{
		if(vi[i]==0)
		{
			Tarjan(i, G);
		}
	}
}
贵校PTA作业,请自行绕道,以免不必要的麻烦

你可能感兴趣的:(C/C++)