[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;
- }
[SOJ] connect components in undirected graph的更多相关文章
- Sicily connect components in undirected graph
题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...
- sicily 4378 connected components in undirected graph
题意:求图中的连通块数,注意孤立的算自连通! 例如:6个顶点3条路径,其中路径为:1->2 4->5 1->3 那么有(1-2&&1->3) + (4- ...
- [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), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [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 ...
- [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), ...
- 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 ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
随机推荐
- zabbix 安装配置以及漏洞检测脚本
最近zabbix爆出sql注入漏洞.之前一直没装过.就想着来安装一次.我在centos配置玩玩,记录一下:1.安装LAMP yum -y install httpd mysql mysql-ser ...
- COCOS2D-JS入门-web端项目部署
下载cocos2d-js文件,建议上官网下载(外国官网或者中国官网都可以) 外国官网:http://cocos2d-x.org/download(选择最新版即可,我下载时为3.9版本,大概300多M) ...
- gimagex 2.0.17 汉化版
软件名称: gimagex 2.0.17 汉化版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 1.31MB 图片预览: 软件简介: gimagex 2. ...
- jQuery中操作Ajax方法小结
有时候,越深入去了解一个点,越发觉得自己无知,而之前当自己晓得一两个片面的点还洋洋自得,殊不知,这是多么讽刺 jQery对Ajax操作进行了封装,常见的 ajax()属于最底层的方法,使用频率很高的 ...
- [SOJ] 畅通工程续
Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多 ...
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- bone collector hdu 01背包问题
Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...
- Python学习日志(一)
1.os - Normal Method: os.name() : os.getcwd(): 给出当前的目录,python当前的工作目录 os.listdir(): 返回 os.remove():删除 ...
- GCD系列 之(二): 多核心的性能
全局队列的并发执行 for(id obj in array) [self doSomethingIntensiveWith:obj]; 假设,每个元素要做的事情-doSomethingIntensiv ...
- 批处理改hosts
@echo off color 0F @attrib -r "%windir%\system32\drivers\etc\hosts" @echo ######测试配置 beg & ...