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

版权声明:本文为博主原创文章,转载前动动小手点个【赞】吧~ https://blog.csdn.net/Dream_Weave/article/details/83536739

题目链接:点击打开链接

题目大意:略。

解题思路:略。

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;
}

猜你喜欢

转载自blog.csdn.net/Dream_Weave/article/details/83536739