[SOJ] connect components in undirected graph
题目描述:
输入一个简单无向图,求出图中连通块的数目
输入:
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。
题目分析:
利用深度优先搜索寻找连通块数,一趟深度优先搜索为一个连通块,深度优先搜索次数为块数。
#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相关推荐
- [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph...
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Codeforces987F AND Graph
点击打开链接 F. AND Graph time limit per test 4 seconds memory limit per test 256 megabytes input standard ...
- codeforces CF986C AND Graph 建圖 dfs
$ \rightarrow $ 戳我進CF原題 C. AND Graph time limit per test: 4 seconds memory limit per test: 256 megab ...
- Clone Graph
题目 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. O ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- Codeforces 920E-Connected Components? (set,补图,连通块)
Connected Components? CodeForces - 920E You are given an undirected graph consisting of n vertices a ...
- 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/ ...
- 【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 ...
- 【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 ...
最新文章
- 【Network Security!】关于DDOS攻击的现状分析与探索
- tableau必知必会之用 Fixed 函数实现客户回购分析
- 销售订单抬头与行项目数据的关系
- el-tree 权限 勾选_一周新债总结,最高涨30%,10月26日起可转债交易需要先开权限...
- 使用Socket模拟聊天室
- oauth基本流程和原理
- CENTOS6.4安装vnc-server
- 创建组_在Allegro软件中的Groups组创建之后怎么进行打散呢?
- 消息中间件学习总结(6)——RocketMQ之RocketMQ大数据畅想
- ASP.NET TextBox 当鼠标点击后清空默认提示文字
- mysql5.6找不到int_为什么在mysql 5.6中,在int字段上自动递增跳过“2147483646”?
- docker启动失败Failed to start Docker Application Container Engine.(centos7)
- 《位置计算:无线网络定位》学习小结
- MySQL主从介绍及配置
- 第三期:ArcMap基础
- java 字符串替换
- 肠道微生物群与过敏性鼻炎
- 魔兽服务器优化,《魔兽世界》画质与流畅最佳优化指南
- 根据ip查询真实地址
- java设置手机后台进程限制_当应用程序在Android Pie上受到后台限制时启动前台服务...