Write a function to count the number of connected components in a given graph.

Format of functions:

int CountConnectedComponents( LGraph Graph );

where LGraph is defined as the following:

typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
}; typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum]; typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;

The function CountConnectedComponents is supposed to return the number of connected components in the undirected Graph.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h> typedef enum {false, true} bool;
#define MaxVertexNum 10 /* maximum number of vertices */
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */ typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
}; typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum]; typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph; LGraph ReadG(); /* details omitted */ int CountConnectedComponents( LGraph Graph ); int main()
{
LGraph G = ReadG();
printf("%d\n", CountConnectedComponents(G)); return 0;
} /* Your function will be put here */

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

8 6
0 7
0 1
2 0
4 1
2 4
3 5

Sample Output:

3
找图的连通分支 代码:
int CountConnectedComponents( LGraph Graph )
{
int vis[MaxVertexNum] = {};
int c = ;
for(int i = ;i < Graph -> Nv;i ++)
{
if(vis[i] == )
{
c ++;
vis[i] = ;
PtrToAdjVNode t;
int head = ,tail = ;
int s[];
s[tail ++] = i;
while(head < tail)
{
t = Graph -> G[s[head]].FirstEdge;
while(t)
{
if(!vis[t->AdjV])
{
vis[t->AdjV] = ;
s[tail ++] = t -> AdjV;
}
t = t -> Next;
}
head ++;
}
}
}
return c;
}

6-19 Count Connected Components(20 分)的更多相关文章

  1. L1-049 天梯赛座位分配 (20 分)

    L1-049 天梯赛座位分配 (20 分)(Java解法) 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所 ...

  2. PAT乙级:1014 福尔摩斯的约会 (20分)

    PAT乙级:1014 福尔摩斯的约会 (20分) 题干 大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk ...

  3. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  4. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  5. 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)

    package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...

  6. Codeforces E - Connected Components?

    E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...

  7. 1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

  8. PAT 1039 到底买不买(20)(20 分)

    1039 到底买不买(20)(20 分) 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要 ...

  9. 1116 Come on! Let's C (20 分)

    1116 Come on! Let's C (20 分) "Let's C" is a popular and fun programming contest hosted by ...

随机推荐

  1. c/c++值传递和引用传递

    今天看数据结构的时候,因为是c语言版的,刚开始学的时候就对指针搞的焦头烂额,今天,发现参数传递的时候,&符号也莫名其妙,搜了一篇好文,转载下来. 一. 函数参数传递机制的基本理论 函数参数传递 ...

  2. MOPSO 多目标粒子群优化算法

    近年来,基于启发式的多目标优化技术得到了很大的发展,研究表明该技术比经典方法更实用和高效.有代表性的多目标优化算法主要有NSGA.NSGA-II.SPEA.SPEA2.PAES和PESA等.粒子群优化 ...

  3. uva11732 Trie转化

    有40001 个单词每个单词长度不超过1000,每个两个单词之间都要比较求要比较次数 int strcmp(char *s,char *t){ int i; for(i = 0; s[i]==t[i] ...

  4. Apache配置WSGI

    Apache配置WSGI 什么是WSGI WSGI被称作web服务器网关接口,在笔者看来其实是基于CGI标准针对Python语言做了一些改进,其主要功能是规范了web 服务器与Pythonj应用程序之 ...

  5. javascript-高级用法

    22.1 安全的类型检测 为什么:typeof 不靠谱, 无法将数组从对象中区分出来, instanceof 有特殊情况,在iframe存在的情况下无法判断另一个iframe内的数组 如何做:Obje ...

  6. 20145311 《Java程序设计》第十周学习总结

    20145311 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 ·网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据·程序员所作的事情就是把数据发送到指定的位置 ...

  7. 20145315 《Java程序设计》第四周学习总结

    20145315 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承与多态 6.1何谓继承 6.1.1继承共同行为 把相同的程序代码提升为父类 private String ...

  8. 20145326《Java程序设计》第一周学习总结

    20145326<Java程序设计>第一周学习总结 教材学习内容总结 转眼间新的一学期终于开始了!为什么我这么期待呢?因为这学期可以上娄嘉鹏老师的java程序设计课,我不是什么电脑天才,之 ...

  9. 导入tensorflow:ImportError: libcublas.so.9.0: cannot open shared object file: No such file or director【转】

    本文转载自:https://blog.csdn.net/ksws0292756/article/details/80034086 版权声明:本文为博主原创文章,转载请一定附上博主原文链接,并署名转自Z ...

  10. 【图片服务器】搭建Nginx图片服务器

    一.安装Nginx 二.安装vsftpd 三.开始搭建Nginx图片服务器 1.效果 例如:图片通过ftp服务上传到/home/ftpuser/www/images目录下,我想通过访问Nginx服务器 ...