Sicily connect components in undirected graph
输入一个简单无向图,求出图中连通块的数目。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。
单独一行输出连通块的数目。
5 3
1 2
1 3
2 4
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的更多相关文章
- [SOJ] connect components in undirected graph
题目描述: 输入一个简单无向图,求出图中连通块的数目 输入: 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以下m行 ...
- 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), ...
随机推荐
- [Design Pattern] Singleton Pattern 简单案例
Singleton Pattern, 即单例模式,用于获取类的一个对象,该对象在整个应用中是其类的唯一对象.单例模式属于创建类的设计模式. SingleObject 作为单例类,内含了一个静态私有的 ...
- LeetCode-Add Two Binary
Add BinaryApr 2 '12 3558 / 10570 Given two binary strings, return their sum (also a binary string). ...
- OBJ-C
1.直接赋值 NSString *name = @"Starain"; 2.用已经存在的字符串进行初始化 NSString *name2 = [NSString stringWit ...
- 在sql语句中使用plsql变量
示例代码如下: create or replace type ua_id_table is table of number; declare v_tab ua_id_table;begin v_tab ...
- windows快捷键和命令
以管理员方式打开命令行界面:win+X+A 打开服务界面:services.msc 删掉windows系统记住的WIFI密码 cmd下面运行 显示存储的无线连接netsh wlan show prof ...
- 【开源项目】Android 手写记事 App(半成品)
该项目已上传到 CSDN 的 Git 平台中 项目地址:https://code.csdn.net/gd920129/whiteboard GIT SSH:git@code.csdn.net:gd92 ...
- Collection子接口(List/Set/Queue/SortedSet)
Collection基本的子接口: List:能够存放反复内容 Set:不能存放反复内容,全部反复的内容靠hashCode()和equals()两个方法区分 Queue:队列接口 SortedSet: ...
- *candy——leetcode
/* */ #include<iostream> #include<vector> //#include<algorithm> #include <windo ...
- PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
What has changed in PHP 5.4.x Most improvements in PHP 5.4.x have no impact on existing code. There ...
- [转] Python 模块学习:os模块
一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.(一语中的) 二.常用方法 1.os.name 输出字符串指示正在使用的平台 ...