6-10 Strongly Connected Components(30 分)

6-10 Strongly Connected Components(30 分)

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 <stdio.h>
#include <stdlib.h>

#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.


一个测试点都过不了..而且只有一个节点时提示段错误

可能是自己写的ReadG()函数和题目不一样,也可能是自己程序本身的问题吧

用的Tarjan算法,仅供参考

int DFN[MaxVertices] = { 0 }, LOW[MaxVertices] = { 0 }, Visit[MaxVertices] = { 0 };
int Stack[MaxVertices], Flag[MaxVertices], Top = -1, Num = 0;


void Tarjan(Graph G, Vertex i)
{
DFN[i] = LOW[i] = ++Num;
Stack[++Top] = i;
Flag[i] = 1;
Visit[i] = 1;
PtrToVNode Ptr = G->Array[i]->Next;
for (; Ptr != NULL; Ptr = Ptr->Next) {
if (!Visit[Ptr->Vert]) {
Tarjan(G, Ptr->Vert);
LOW[i] = LOW[i] > LOW[Ptr->Vert] ? LOW[Ptr->Vert] : LOW[i];
}
else if (Flag[Ptr->Vert] == 1)
LOW[i] = LOW[Ptr->Vert] > DFN[i] ? DFN[i] : LOW[Ptr->Vert];
}
if (DFN[i] == LOW[i]) {
while (DFN[Stack[Top]]!=LOW[Stack[Top]]) {
Flag[Stack[Top]] = 0;
printf("%d ", Stack[Top--]);
}
Flag[Stack[Top]] = 0;
printf("%d ", Stack[Top--]);
printf("\n");
}
}


void StronglyConnectedComponents(Graph G, void(*visit)(Vertex V))
{
for (int i = 0; i < G->NumOfVertices; i++) 
if (!Visit[i])
Tarjan(G,i);
}


附上自己写的ReadG():

Graph ReadG()
{
int N1, N2;
Graph G = (Graph)malloc(sizeof(struct GNode));
scanf("%d%d", &G->NumOfVertices, &G->NumOfEdges);
G->Array = (PtrToVNode*)malloc(G->NumOfVertices * sizeof(PtrToVNode));
for (int i = 0; i < G->NumOfVertices; i++) {
G->Array[i] = (PtrToVNode)malloc(sizeof(struct VNode));
G->Array[i]->Vert = i;
G->Array[i]->Next = NULL;
}
for (int i = 0; i < G->NumOfEdges; i++) {
scanf("%d%d", &N1, &N2);
PtrToVNode Ptr = (PtrToVNode)malloc(sizeof(struct VNode));
Ptr->Vert = N2;
Ptr->Next = NULL;
if (G->Array[N1]->Next == NULL)
G->Array[N1]->Next = Ptr;
else {
Ptr->Next = G->Array[N1]->Next;
G->Array[N1]->Next = Ptr;
}
}
return G;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值