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. Hadoop mapreduce自定义分组RawComparator

    本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...

  2. cas php

    CAS的php客户端实践—单点登录整合php程序 兄弟近日尝试将一个php程序以单点登录方式和原有的系统整合在一起.验证服务器选用的是CAS,其提供有相应的php客户端.整个过程如下:1.搭建CAS服 ...

  3. hdu5021 树状数组+二分

    这 题 说 的 是 给 了 一 个 K—NN    每次查询离loc 最近的k个数 然后将这k个数的权值加起来除以k 赋值给 loc 这个位置上的 权值  我说说 我的做法 假如 查询的是loc 这个 ...

  4. JavaScript高级内容笔记:原型链、继承、执行上下文、作用域链、闭包

    最近在系统的学习JS深层次内容,并稍微整理了一下,作为备忘和后期复习,这里分享给大家,希望对大家有所帮助.如有错误请留言指正,tks. 了解这些问题,我先一步步来看,先从稍微浅显内容说起,然后引出这些 ...

  5. 20145303刘俊谦《网络对抗》Exp2 后门原理与实践

    20145303刘俊谦<网络对抗>Exp2 后门原理与实践 基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式? •在网页上浏览不安全的网站或者下载不安全的软件 •通过发送邮 ...

  6. DirectX9.0c SDK学习笔记(一)

    Direct9.0c SDK中提供了一个叫DXviewer的*.x格式文件查看器的源码,代码给出了基于DXUT框架的模型显示接口使用方法, 对于我想编写一个动作捕捉的上位程序是大有助益的. 我的想法是 ...

  7. 记一次Servlet中getAttribute的错误.

    package com.ykmimi.order.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher ...

  8. div框选中状态,倒三角样式

    html代码: <html> <head> <style> #triangle-bottomright { width:0; height: 0; display: ...

  9. DDMS

    DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务

  10. 快捷方式控制台调试each这种方法的时候怎么停

    1.ctrl +' 2.当遇到angular.each的时候ctrl + ; 3.进入之后,还是先ctrl+'; angular中: 当遇到forEach之后,又一次ctrl+;就回到你的each之后 ...