#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
typedef struct ArcNode {
int adjvex;
struct ArcNode *next;
} ArcNode;
typedef struct VNode {
int data;
ArcNode *first;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
int visited[MAX_VERTEX_NUM];
void DFS(ALGraph G, int v) {
visited[v] = 1;
ArcNode *p = G.vertices[v].first;
while (p != NULL) {
if (!visited[p->adjvex]) {
DFS(G, p->adjvex);
}
p = p->next;
}
}
int CountConnectedComponents(ALGraph G) {
int count = 0;
for (int i = 0; i < G.vexnum; i++) {
if (!visited[ i]) {
count++;
DFS(G, i);
}
}
return count;
}
int main() {
// 初始化图 G
ALGraph G;
// 调用CountConnectedComponents函数求出连通分量个数
int result = CountConnectedComponents(G);
printf("连通分量个数为:%d\n", result);
return 0;
}