题目介绍:

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

Input

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

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

单独一行输出连通块的数目。

Sample Input
5 3
1 2
1 3
2 4
Sample Output
2

思路:

利用广度搜索,计算广度搜索的次数即为结果。

具体代码如下:

 #include <iostream>
#include <queue>
using namespace std; bool path[][];
bool visited[]; int main() {
int n, m;
cin >> n >> m; for (int i = ; i < m; i++) {
int node1, node2;
cin >> node1 >> node2;
path[node1][node2] = true;
path[node2][node1] = true;
} for (int i = ; i <= n; i++) {
visited[i] = false;
} int count = ;
int temp = n;
while (temp--) {
queue<int> store;
for (int i = ; i <= n; i++) {
if (!visited[i]) {
store.push(i);
count++;
visited[i] = true;
break;
}
} while (!store.empty()) {
for (int i = ; i <= n; i++) {
if (path[store.front()][i] && !visited[i]) {
store.push(i);
visited[i] = true;
}
}
store.pop();
}
} cout << count << endl; return ;
}

Sicily connect components in undirected graph的更多相关文章

  1. [SOJ] connect components in undirected graph

    题目描述: 输入一个简单无向图,求出图中连通块的数目 输入: 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以下m行 ...

  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. Android Memory Leak

    线程也是造成内存泄露的一个重要的源头.线程产生内存泄露的主要原因在于线程生命周期的不可控.1.看一下下面是否存在问题 public class ThreadActivity extends Activ ...

  2. 关于在SLES11, RHEL6, OEL6 and UEK2 Kernels使用hugepages的告警

    ALERT: Disable Transparent HugePages on SLES11, RHEL6, OEL6 and UEK2 Kernels (Doc ID 1557478.1) Modi ...

  3. Jenkins的错误“error fetching remote repo origin”的问题解决

    错误如上,解决方法收集,可以尝试以下方法: http://stackoverflow.com/questions/38391601/jenkins-error-error-fetching-remot ...

  4. Sublime text 3 格式化HTML/css/js/json代码 插件

    JsFormat: 这里下载这插件包 https://github.com/jdc0589/JsFormat ,点油下角的zip就能下载 插件包放到sublime安装目录的Data\Packages目 ...

  5. mysql查询最近一小时的数据

    date_sub()函数: DATE_SUB(date,INTERVAL expr type) 实例: SELECT NOW(),DATE_SUB(NOW(),INTERVAL HOUR) as th ...

  6. 递归实现生成Grey码

    腾讯2016研发笔试题1: 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同, 则称这种编码为格雷码(Gray Code),请编写一个函数,使用递归的方法生成N位的格雷码. 给定一个整数n, ...

  7. Unity3D NGUI UIPlayTween控件(一)动态打开关闭窗口

    利用NGUI自带的脚本控件实现按钮点击窗口滑动出现与隐藏. 创建界面 首先建立如下图的三个BUtton与三个Panel 绑定脚本 然后在每个Button上添加UIPlayTween脚本,在Intera ...

  8. Android Spinner列表选择框

    Spinner Spinner是一个下拉列表,通常用于选择一系列可选择的列表项,它可以使用适配器,也可以直接设置数组源. 1.直接设置数组源 在res/values/strings.xml中设置数组源 ...

  9. Ellipse常用快捷键

    Ctrl+m:视窗大小变化 Ctrl+F6:在打开的文件件进行切换 Ctrl+F7:在资源窗口间切换Ctrl+F8:在各种模式下进行切换 Ctrl+e:选择某个打开的文件Shift+home:整行选取 ...

  10. Embedded tomcat 7 servlet 3.0 annotations not working--转

    Question: I have a stripped down test project which contains a Servlet version 3.0, declared with an ...