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), ...
随机推荐
- UVAlive3713 Astronauts(2-SAT)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18511 [思路] 2-SAT. 设分得A或B类任务为1 C类任务为 ...
- cloudCompute
云计算使企业可以迅速对市场做出反应而且加强了企业内部的协同 人人都在用云计算的时候能够迅速迁移到云平台已经不叫优势了,反而不使用云计算成为了一种劣势
- 终极shell zsh
在mac上安装zsh,推荐安装. 参见http://macshuo.com/?p=676. 安装成功提示,看着很帅的样子
- iOS 通过个推 推送原理
目前使用过的第三方推送很多,有极光, 友盟,个推等,现在主要针对个推,谈谈我对推送流程的理解. 在项目中,如果想要实现评论 推送功能 需要进行以下步骤: 1. 在用户登录的时候 通过 [GeTui ...
- AIX Study之--AIX网卡配置管理(ent0、en0、et0)
AIX Study之--AIX网卡配置管理(ent0.en0.et0) 1.查看AIX系统网卡信息: [root@aix211 /]#lsdev |grep et en0 Available 1L- ...
- 真正菜鸟用教程之WQSG Scrip Export WQSG (脚本导出导入工具,PSP、NDS汉化必备 )
先扫盲WQSG是干什么用的 一些掌机类游戏汉化比方PSP NDS 汉化必备之物 它能够依据字典转换文本 假设你不知道这是啥玩意,快去充电染成茜色的坂道 文本提取(导出)方法 (下文称导出文章) 在导出 ...
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)
MySQL主从复制(Master-Slave)即同步与读写分离(MySQL-Proxy)即集群
- [CSAPP笔记][第九章虚拟存储器][吐血1500行]
9.虚拟存储器 为了更加有效地管理存储器且少出错,现代系统提供了对主存的抽象概念,叫做虚拟存储器(VM). 虚拟存储器是硬件异常,硬件地址翻译,主存,磁盘文件和内核软件的完美交互. 为每个进程提供一个 ...
- 手势识别 GestureDetector ScaleGestureDetector
识别器GestureDetector基本介绍 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等.一般情况下,我们可以通过View或Activity的onTouch ...
- 使用socket实现聊天功能
public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSocket=null ...