题目描述:

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

输入:

输入的第一行包含两个整数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. JavaEE XML SAX解析

    SAX解析XML @author ixenos SAX解析工具 SAX解析工具-  Sun公司提供的.内置在jdk中.org.xml.sax.* 核心的API: SAXParser类: 用于读取和解析 ...

  2. [Q]手动加载菜单方法

    一般情况下,安装程序会自动安装依云软件菜单,但可能由于某些原因未能自动安装的话,您可以手动加载菜单,步骤如下: 在AoutCAD命令行输入"CUILOAD",会弹出"加载 ...

  3. CG 标准函数库

    (1)数学函数 函数 功能描述 abs(x) 返回输入参数的绝对值 acos(x) 反余切函数,输入参数范围为[-1,1], 返回[0,π]区间的角度值 all(x) 如果输入参数均不为0,则返回tu ...

  4. OpenCV备忘

    都是转来的内容的,算是整理一下 OpenCV备忘 深度和通道的理解 CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵 CV_8UC1 CV_8SC1 ...

  5. nginx(2)

    上一篇: nginx(1) 负载均衡: linux集群的一种常见方式,即由多台服务器组成一个服务器集合实现某个特定需求,其中每台服务器都是等价的,从而实现负载均摊的目的. 反向代理: 是指以代理服务器 ...

  6. 开学&东大一周游记

    明天就要离开生活但并没有学到多少东西的东大了,不舍,这是真的,因为真的是没学到多少就要走了.但是终归是有收获的,比如感受到了舍长这样的大牛的学习态度,东大的浴池真的很棒,我很感激吉大的伙食诸如此类.感 ...

  7. 基于React Native的58 APP开发实践

    React Native在iOS界早就炒的火热了,随着2015年底Android端推出后,一套代码能运行于双平台上,真正拥有了Hybrid框架的所有优势.再加上Native的优秀性能,让越来越多的公司 ...

  8. 图片上传插件用法,net语法【二】

    之前一直写过KindeEditor中的小控件作为单独上次,但业务要求需要另一种方式 现在改用ajaxfileupload.js试试,这个一百度 一.首页引用 <script src=" ...

  9. PQ分区魔术师v9.0 中文版

    软件名称: pqmagic 硬盘分区大师9.0中文绿色版 软件大小:5.80MB 软件语言:简体中文 软件类别:磁盘工具 软件授权:免费软件 更新时间:2013-10-082013-10-08 09: ...

  10. wampserver使用过程中遇到的问题及相关配置

    wampserver集合了Apache+PHP+Mysql 下载地址:点这里 安装过程很方便,一直点下一步就行,中间会提示选择默认的浏览器及文件编辑器 起因: 使用过程中提示: In the http ...