题目描述:

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

输入:

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

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

题目分析:

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

  1. #include<iostream>
  2. #include<memory>
  3. using namespace std;
  4.  
  5. const int MAX=1001;
  6. int edge[MAX][MAX];
  7. int n, m;
  8. int num=0;
  9. bool isvisited[MAX];
  10.  
  11. void DFS(int current)
  12. {
  13. for(int i=1;i<=n;i++)
  14. {
  15. if(!isvisited[i]&&edge[current][i])
  16. {
  17. isvisited[i]=true;
  18. DFS(i);
  19. }
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. cin>>n>>m;
  26.  
  27. int a, b;
  28.  
  29. //初始化
  30. memset(edge, 0,sizeof(edge));
  31. memset(isvisited, false, sizeof(isvisited));
  32.  
  33. for(int i=0;i<m;i++)
  34. {
  35. cin>>a>>b;
  36. edge[a][b]=1;
  37. edge[b][a]=1;
  38. }
  39.  
  40. for(int i=1;i<=n;i++)
  41. {
  42. if(!isvisited[i])
  43. {
  44. num++;
  45. isvisited[i]=true;
  46. DFS(i);
  47. }
  48. }
  49.  
  50. cout<<num<<endl;
  51. return 0;
  52. }

  

[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. zabbix 安装配置以及漏洞检测脚本

    最近zabbix爆出sql注入漏洞.之前一直没装过.就想着来安装一次.我在centos配置玩玩,记录一下:1.安装LAMP yum -y install httpd  mysql  mysql-ser ...

  2. COCOS2D-JS入门-web端项目部署

    下载cocos2d-js文件,建议上官网下载(外国官网或者中国官网都可以) 外国官网:http://cocos2d-x.org/download(选择最新版即可,我下载时为3.9版本,大概300多M) ...

  3. gimagex 2.0.17 汉化版

    软件名称: gimagex 2.0.17 汉化版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 1.31MB 图片预览: 软件简介: gimagex 2. ...

  4. jQuery中操作Ajax方法小结

    有时候,越深入去了解一个点,越发觉得自己无知,而之前当自己晓得一两个片面的点还洋洋自得,殊不知,这是多么讽刺 jQery对Ajax操作进行了封装,常见的 ajax()属于最底层的方法,使用频率很高的 ...

  5. [SOJ] 畅通工程续

    Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多 ...

  6. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  7. bone collector hdu 01背包问题

    Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...

  8. Python学习日志(一)

    1.os - Normal Method: os.name() : os.getcwd(): 给出当前的目录,python当前的工作目录 os.listdir(): 返回 os.remove():删除 ...

  9. GCD系列 之(二): 多核心的性能

    全局队列的并发执行 for(id obj in array) [self doSomethingIntensiveWith:obj]; 假设,每个元素要做的事情-doSomethingIntensiv ...

  10. 批处理改hosts

    @echo off color 0F @attrib -r "%windir%\system32\drivers\etc\hosts" @echo ######测试配置 beg & ...