题目描述:

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

输入:

输入的第一行包含两个整数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;
}

  

转载于:https://www.cnblogs.com/KennyRom/p/6243859.html

[SOJ] connect components in undirected graph相关推荐

  1. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph...

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  2. Codeforces987F AND Graph

    点击打开链接 F. AND Graph time limit per test 4 seconds memory limit per test 256 megabytes input standard ...

  3. codeforces CF986C AND Graph 建圖 dfs

    $ \rightarrow $ 戳我進CF原題 C. AND Graph time limit per test: 4 seconds memory limit per test: 256 megab ...

  4. Clone Graph

    题目 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. O ...

  5. 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

    背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...

  6. Codeforces 920E-Connected Components? (set,补图,连通块)

    Connected Components? CodeForces - 920E You are given an undirected graph consisting of n vertices a ...

  7. LintCode Find the Weak Connected Component in the Directed Graph

    原题链接在这里:http://www.lintcode.com/en/problem/find-the-weak-connected-component-in-the-directed-graph/ ...

  8. 【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)

    题干: You are given an undirected graph consisting of n vertices and  edges. Instead of giving you the ...

  9. 【CF#505B】Mr. Kitayuta's Colorful Graph (并查集或Floyd或BFS)

    题干: Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The verti ...

最新文章

  1. 【Network Security!】关于DDOS攻击的现状分析与探索
  2. tableau必知必会之用 Fixed 函数实现客户回购分析
  3. 销售订单抬头与行项目数据的关系
  4. el-tree 权限 勾选_一周新债总结,最高涨30%,10月26日起可转债交易需要先开权限...
  5. 使用Socket模拟聊天室
  6. oauth基本流程和原理
  7. CENTOS6.4安装vnc-server
  8. 创建组_在Allegro软件中的Groups组创建之后怎么进行打散呢?
  9. 消息中间件学习总结(6)——RocketMQ之RocketMQ大数据畅想
  10. ASP.NET TextBox 当鼠标点击后清空默认提示文字
  11. mysql5.6找不到int_为什么在mysql 5.6中,在int字段上自动递增跳过“2147483646”?
  12. docker启动失败Failed to start Docker Application Container Engine.(centos7)
  13. 《位置计算:无线网络定位》学习小结
  14. MySQL主从介绍及配置
  15. 第三期:ArcMap基础
  16. java 字符串替换
  17. 肠道微生物群与过敏性鼻炎
  18. 魔兽服务器优化,《魔兽世界》画质与流畅最佳优化指南
  19. 根据ip查询真实地址
  20. java设置手机后台进程限制_当应用程序在Android Pie上受到后台限制时启动前台服务...

热门文章

  1. wpf加载obj格式的3D模型图解
  2. SNMP协议介绍和操作截图
  3. 【C++标准库】特殊容器
  4. JS转换HTML转义符
  5. 心辽阔了,人生才能辽阔
  6. 定义一个大整数类,并重载乘法*运算符
  7. 程序员求职之道(《程序员面试笔试宝典》)之面试官箴言?
  8. pureMVC介绍及学习
  9. 如何通过反射调用扩展方法?
  10. flex+hibernate 中java的pojo与as的pojo的映射问题