题目描述:

输入一个简单无向图,求出图中连通块的数目

输入:

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。

以下m行,每行是一个数对v y,表示存在边(v,y)。顶点编号从1开始。

题目分析:

利用深度优先搜索寻找连通块数,一趟深度优先搜索为一个连通块,深度优先搜索次数为块数。

#include<iostream>
#include<memory>
using namespace std; const int MAX=1001;
int edge[MAX][MAX];
int n, m;
int num=0;
bool isvisited[MAX]; void DFS(int current)
{
for(int i=1;i<=n;i++)
{
if(!isvisited[i]&&edge[current][i])
{
isvisited[i]=true;
DFS(i);
}
}
} int main()
{
cin>>n>>m; int a, b; //初始化
memset(edge, 0,sizeof(edge));
memset(isvisited, false, sizeof(isvisited)); for(int i=0;i<m;i++)
{
cin>>a>>b;
edge[a][b]=1;
edge[b][a]=1;
} for(int i=1;i<=n;i++)
{
if(!isvisited[i])
{
num++;
isvisited[i]=true;
DFS(i);
}
} cout<<num<<endl;
return 0;
}

  

[SOJ] connect components in undirected graph的更多相关文章

  1. Sicily connect components in undirected graph

    题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...

  2. sicily 4378 connected components in undirected graph

    题意:求图中的连通块数,注意孤立的算自连通! 例如:6个顶点3条路径,其中路径为:1->2    4->5  1->3 那么有(1-2&&1->3) + (4- ...

  3. [LeetCode] 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), ...

  4. LeetCode Number of Connected Components in an Undirected Graph

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

  5. [Locked] Number of Connected Components in an Undirected Graph

    Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...

  6. [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), ...

  7. 323. 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 n ...

  8. LeetCode 323. Number of Connected Components in an Undirected Graph

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

  9. 323. Number of Connected Components in an Undirected Graph (leetcode)

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

随机推荐

  1. visual studio 中将选中代码相同的代码的颜色设置,修改高亮颜色

    这是一个很实用的功能,默认的设置里不是很明显,设置完之后效果图如下: 具体设置方法是: 1. 菜单:工具  -> 选项  ->环境  ->字体和颜色 2. 在右边的 "显示 ...

  2. CentOS 添加本地yum源

    1 创建保存RPM包的路径: mkdir -p /share/CentOS/7/local/x86_64/RPMS 2 安装createrepo工具 yum install createrepo 3 ...

  3. myBatis系列之七:事务管理

    myBatis系列之七:事务管理 如果在操作时,如果运行时错误自动进行回滚,需要以下两个配置 @Transactional()public void save(User user) { userDao ...

  4. 转:NSString什么时候用copy,什么时候用strong

    大部分的时候NSString的属性都是copy,那copy与strong的情况下到底有什么区别呢? 比如: @property (retain,nonatomic) NSString *rStr; @ ...

  5. 圆形图片CustomShapeImageView

    第三方控件 [GitHub的源码下载] (https://github.com/MostafaGazar/CustomShapeImageView) 1:依赖包 dependencies { ... ...

  6. C# 语言规范_版本5.0 (第8章 语句)

    1. 语句 C# 提供各种语句.使用过 C 和 C++ 编程的开发人员熟悉其中大多数语句. statement: labeled-statement declaration-statement emb ...

  7. C#基础1:Console类

    Console相关:   1.输出到控制台 Console.Write(输出的值);  表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入.Console.WriteLine(输出的值) ...

  8. 【翻译】创建Cordova项目

    下载或者更新Node.js到最新版本 在电脑终端输入命令来安装Cordova CLI sudo npm install -g cordova 如果使用Windows电脑,需要在使用cmd命令行输入 n ...

  9. 利用GCD实现单利模式的宏代码

    以下是.h文件,使用时,直接在需要实现单例模式的类中导入头文件即可. // .h文件 #define DenglSingletonH(name) + (instancetype)shared##nam ...

  10. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...