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), ...
随机推荐
- Easyui弹出窗体在iframe的父级页面显示
今天做EasyUI学习的预到了一个这样的问题:通过iframe加载的一个页面在调用$.messager.alert();这个方法后只能在iframe中显示alert效果而不是在全局的页面上显示这并不我 ...
- git查看某个文件的修改历史
<转自 http://www.cnblogs.com/flyme/archive/2011/11/28/2265899.html> 有时候在比对代码时,看到某些改动,但不清楚这个改动的作者 ...
- Python基础类型
1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ['Alex',"Tenglan",'Eric ...
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 II ...
- MVC接收以post形式传输的各种参数
近日研究用wcf框架接收同事Android端以post形式传输的各种类型的参数,来做处理.但研究过程中问题比较多,首先键值对的形式是实现了,传输int,string类型都不成问题,但是到传输文件的时候 ...
- Ecstore内置表单验证?
- linux下查看所有用户及所有用户组
groups 查看当前登录用户的组内成员groups gliethttp 查看gliethttp用户所在的组,以及组内成员whoami 查看当前登录用户名 /etc/group文件包含所有组/etc/ ...
- Linux && vim 批量替换
Linux批量文件的字符串替换 sed -i "s/oldstring/newstring/g" `grep oldstring -rl path` vim多行替换::1,2s/s ...
- oralce中exists not exists in not in对于NULL的处理
1. 先讨论 in 与 not in中存在NULL的情况, sql语句如下: 1 select 1 result1 from dual where 1 not in (2, 3); 2 3 4 s ...
- 批处理备份和恢复mysql数据库
备份 set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%" md "D: ...