题目描述:

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

输入:

输入的第一行包含两个整数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. java zip 批量打包(java.util包和apache.tools包)

    /** * 文件批量打包 * @param zipPath 打包路径 * @param files 批量文件 */ public void zipOut(String zipPath,File[] f ...

  2. android studio 工程设置项

    1.在<工程根目录>\build\generated文件夹中 存在 xxx.jar 这个文件是用来做单元测试用的,但该功能目前还处于实验阶段,不想用可以关了. 去掉勾后,clean一下工程 ...

  3. 接口测试:如何定位BUG的产生原因

    转自公众号<QA之道> 我们从在日常功能测试过程中对UI的每一次操作说白了就是对一个或者多个接口的一次调用,接口的返回的内容(移动端一般为json)经过前端代码的处理最终展示在页面上.ht ...

  4. openstack私有云布署实践【17 配置文件部份说明】

    Nova部份 cpu_allocation_ratio = 4.0 物理 CPU 超售比例,默认是 16 倍,超线程也算作一个物理 CPU,需要根据具体负载和物理 CPU 能力进行综合判断后确定具体的 ...

  5. Postman使用教程学习笔记

    刚加入网页测试行列,最近在学习POSTman的使用教程,记录下学习笔记. Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件.当开发人员需要调试一个网页是否运行正常,并不是 ...

  6. SAP HANA创建层次结构的属性视图(Hierarchy Attribute View)

    在产品表中,数据是具有一定层次结构的 1. 在Content相应的package下面右键点击Attribute View,新建Attribute View 填写相关信息,注意:我们所建的依然是标准视图 ...

  7. Oracle 字符集小结(遇到一例子:查询结果列标题为汉字,但是显示为‘?')

    问题处理方式: 查询:select userenv('language') from dual; 对比电脑环境变量NLS_LANG的值与查询结果是否一致,如果不一致,修改电脑环境变量NLS_LANG ...

  8. js一些小知识点

    1.isNaN(),里面传一个参数,用来判断传入的值是否是数字.可以用来做一些简单的表单判断. 2.用innerHTML属性可以操作(包括获取和设置)span的内容,实际上对所有非表单元素都可以用in ...

  9. 原生js怎么为动态生成的标签添加各种事件

    这几天用zepto.js写了不少事件,突然想到一个问题,那就是原生的js如何给动态生成的标签添加事件?因为这些标签都是后来通过ajax或者运行其他点击事件生成的,那么如果之前给他们写事件他们这个dom ...

  10. C#动态编程

    class Program { static void Main(string[] args) { Test(); } public static void Test() { //声明代码的部分 Co ...