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), ...
随机推荐
- Android SwitchButton(滑动开关)
@RemoteView public class Button extends TextView { public Button(Context context) { this(context, nu ...
- [转] When exactly does the virtual table pointer (in C++) gets set for an object?
PS: http://stackoverflow.com/questions/7934540/when-exactly-does-the-virtual-table-pointer-in-c-gets ...
- 队列(链式存储)JAVA代码
publicclassLinkQueue<T>{ //结点类 publicclassNode{ public T data; pub ...
- Oracle 空间管理
表空间:组数据文件的一种途径 分类: 目录表空间(sysaux) 常表空间(system) 系统临时表空间(temp) 用户临时表空间(user) undo表空间 创建表空间: //表空间名为name ...
- java的@see注释
@see注释用法 @see 类名 @see #方法名或属性名 @see 类名#方法名或属性名
- mysql的distinct理解
select distinct id,name from route where update_time>=''; 上面的sql语句的逻辑是两条记录的id,name只要有一个不一样,就算不一样. ...
- linux执行文件命令
1.如果path中有你的程序所在的目录,那么直接执行filename即可 2.如果path中没有程序所在目录,那么进入目录./filename或者path/filename 比如 wj@ubuntu: ...
- Swift - 34 - 闭包的基础语法
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...
- Skin++ 皮肤库 CCheckListBox MFC 界面风格
今天使用CCheckListBox,发现增加进去的字符串无法显示,但是当点击的时候,确有反应. 仔细检查代码,没有问题.之前也是这样用的,完全没有问题. 思前想后,觉得是因为使用了Skin++皮肤库, ...
- C++面试题:++i和i++哪个效率高?
1.当变量i的数据类型是c++语言默认提供的类型的话,他们的效率是一样的. 从其汇编执行的条数是一样的,所以其执行效率是一样的(有兴趣可以用gdb查看汇编代码) 2.我们自定的数据类型,++i效率高 ...