题意:求图中的连通块数,注意孤立的算自连通!

例如: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的更多相关文章

  1. Connected Component in Undirected Graph

    Description Find connected component in undirected graph. Each node in the graph contains a label an ...

  2. Sicily connect components in undirected graph

    题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...

  3. [SOJ] connect components in undirected graph

    题目描述: 输入一个简单无向图,求出图中连通块的数目 输入: 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以下m行 ...

  4. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces 920 E Connected Components?

    Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving ...

  6. 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 ...

  7. CodeForces242D:Connected Components (不错的并查集)

    We already know of the large corporation where Polycarpus works as a system administrator. The compu ...

  8. D. Connected Components Croc Champ 2013 - Round 1 (并查集+技巧)

    292D - Connected Components D. Connected Components time limit per test 2 seconds memory limit per t ...

  9. [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), ...

随机推荐

  1. git查看某个文件的修改历史

    <转自 http://www.cnblogs.com/flyme/archive/2011/11/28/2265899.html> 有时候在比对代码时,看到某些改动,但不清楚这个改动的作者 ...

  2. 零基础学习云计算及大数据DBA集群架构师【Linux Bash Shell编程及系统自动化2015年1月21日周四】

    lvy老师教项目课程,以及代课了shell部分课程,大家都觉得这位老师不行,上课时做的操作很多都是错误的,觉得她基础不好.而且,她不能解释原因,学生问为什么,她不知道.崩溃啊.向xx培训机构反应后,说 ...

  3. HDU 1199 - Color the Ball 离散化

    [题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后 ...

  4. 单例模式 与lock用法

    在之前没用lock之前:如果我实现单例模式:直接就是下面的代码: public class Singleton    {        private static Singleton instanc ...

  5. django安装

    见 http://jingyan.baidu.com/article/466506580e7d29f549e5f8b6.html 下载安装python下载解压django cmd进入django目录, ...

  6. php中curl、fsockopen的应用

    最近要用到通过post上传文件,网上盛传的有curl的post提交和fsockopen,其中curl最简单,于是从最简单的说起. 这是简单的将一个变量post到另外一个页面 $url = ''; $d ...

  7. 如果设置http.get超时控制

    var timeout_wrapper = function (req) { return function () { // do some logging, cleaning, etc. depen ...

  8. Oracle数据库之PL/SQL流程控制语句

    Oracle数据库之PL/SQL流程控制语句 在任何计算机编程语言(如C,Java,C#等)都有各种流程控制语句,同样,在PL/SQL中也存在这样的流程控制结构. 几种常见的流程控制结构: 一.条件结 ...

  9. java Socket 长连接 心跳包 客户端 信息收发 demo

    今天写了个socket的测试小程序,代码如下 import java.io.IOException; import java.io.InputStream; import java.io.Output ...

  10. 百度touch的手势框架,touch.js

    今天,随便搜搜看到了一个新的手势库,也许能让我为现在使用者的hammer.js的手势库带来的烦恼而消除. 它是百度团队开发的,现在由百度云Clouda进行维护. 官网   http://touch.c ...