sicily 4378 connected components in undirected graph
题意:求图中的连通块数,注意孤立的算自连通!
例如:6个顶点3条路径,其中路径为:1->2 4->5 1->3
那么有(1-2&&1->3) + (4->5) + (6) 共3个连通块!
解法:对每个节点宽搜!
#include<iostream>
#include<memory.h>
#include<queue> using namespace std; bool roads[][];
bool visited[];
int N,M; int main(){ cin >>N >>M;
memset(roads,,sizeof(roads));
memset(visited,false,sizeof(visited));
int from,dest;
for(int i=; i<=M; i++){
cin >> from >> dest;
roads[from][dest] = true;
roads[dest][from] = true;
} queue<int> check;
int num = ;
int cnt = ;
int i;
//breadth-frist search
while(num != N){
for( i=; i<=N;i++){
if(visited[i]== false){
check.push(i);
visited[i]= true;
num++;
cnt++;
break;
}
}
while(!check.empty()){
i = check.front();
for(int j = ; j<=N;j++){
if(roads[i][j] == true && visited[j] == false){
check.push(j);
visited[j] = true;
num++;
}
}
// erase the front node
check.pop();
}
}
cout << cnt <<endl;
return ;
}
sicily 4378 connected components in undirected graph的更多相关文章
- Connected Component in Undirected Graph
Description Find connected component in undirected graph. Each node in the graph contains a label an ...
- Sicily connect components in undirected graph
题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...
- [SOJ] connect components in undirected graph
题目描述: 输入一个简单无向图,求出图中连通块的数目 输入: 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以下m行 ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论
E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Inste ...
- CodeForces242D:Connected Components (不错的并查集)
We already know of the large corporation where Polycarpus works as a system administrator. The compu ...
- D. Connected Components Croc Champ 2013 - Round 1 (并查集+技巧)
292D - Connected Components D. Connected Components time limit per test 2 seconds memory limit per t ...
- [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), ...
随机推荐
- ORACLE函数之单行数字函数
1. ABS(X) 返回X的绝对值 SQL>SELECT ABS(-1) A,ABS(1) B,ABS(0) C FROM DUAL; A B ...
- php开发中将远程图片本地化的方法
检查文本内容中的远程图片,下载远程图片到本地的方法示例. /** * 下载远程图片到本地 * * @param string $txt 用户输入的文字,可能包含有图片的url * @param str ...
- [转] JS nodeType返回类型
将HTML DOM中几个容易常用的属性做下记录: nodeName.nodeValue 以及 nodeType 包含有关于节点的信息. nodeName 属性含有某个节点的名称. 元素节点的 node ...
- 二叉树(二叉链表实现)JAVA代码
publicclassTest{ publicstaticvoid main(String[] args){ char[] ch =newchar[]{'A','B ...
- sass笔记-1|Sass是如何帮你又快又好地搞定CSS的
Sass学习笔记持续整理中,开篇不讲怎么安装,sass是什么,这些搜索引擎会告诉你,我们从sass的作用开始讲起,知道sass用来干什么,有什么作用,我们才能相信用sass的好处,并且时时刻刻想着sa ...
- OD: DEP - Ret2Libc via VirtualProtect() & VirtualAlloc()
一,通过 VirutalProtect() 修改内存属性绕过 DEP DEP 的四种工作模式中,OptOut 和 AlwaysOn 下所有进程默认都开启 DEP 保护,这里如果一个程序自身需要从堆栈中 ...
- Ecstore关于finder的默认的参数row的数据不见了的一些小问题?
在finder中,我们经常对默认的参数row进行数据的编辑处理,然而,在实际处理中,会遇到这么一个问题,该处理的数据不见了,造成这一原因的重要原 因是因为在设置的时候,把某些字段屏蔽掉了,导致返回的r ...
- 工作中部署使用MP平台的一些问题
1.首先先把项目导入到myeclipse中,如果没有.classpath和.mymetadata和.project等文件,就自己创建一个web项目,然后把里面的src覆盖,webroot等文件覆盖. ...
- 启用DHCP中继代理,实现跨子网服务 - Win 2003 Server
伴随着局域网规模的逐步扩大,一个网络常常会被划分成多个不同的子网,以便根据不同子网的工作要求来实现个性化的管理要求.考虑到规模较大的局域网一般会使用DHCP服务器来为各个工作站分配IP地址,不过一旦局 ...
- 前端/html5效果收藏
H5应用 9款漂亮的H5效果 8款漂亮的H5效果 36漂亮的button效果 颜色RGB表 省市二级联动