Data Structures and Algorithms (English) - 6-14 Count Connected Components(20 分)

阅读 92

2022-05-23


题目链接:​​点击打开链接​​

题目大意:略。

解题思路:略。

AC 代码

int vis[MaxVertexNum];

void dfs(LGraph Graph, int v)
{
vis[v]=1;
PtrToAdjVNode p=Graph->G[v].FirstEdge;
while(p)
{
int i=p->AdjV;
if(!vis[i]) dfs(Graph,i);
p=p->Next;
}
}

int CountConnectedComponents( LGraph Graph )
{
int cnt=0;
for(int i=0;i<Graph->Nv;i++) vis[i]=0;
for(int i=0;i<Graph->Nv;i++)
if(!vis[i])
{
dfs(Graph,i);
cnt++;
}

return cnt;
}


精彩评论(0)

0 0 举报