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

例如: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. IntelliJ IDEA 中module的dependencies是其它module时的注意事项

    Dependencies on other modules If a module (module A) depends on another module (module B), IntelliJ ...

  2. OD: File Vulnerabilities & Protocols & Fuzz

    IE.Office 等软件有个共同点,即用文件作为程序的主要输入,但攻击者往往会挑战程序员的假定和假设. 文件格式 Fuzz 就是利用畸形文件测试软件的稳健性,其流程一般包括: * 以一个正常文件作为 ...

  3. javascript基础之自执行函数

    1.匿名函数的定义方式 如下 var temp = function(){} 2.自执行函数 (function(){             内容    })        () 不带参数 (fun ...

  4. java里面List和Array的区别是什么?

    java里面的List和Array的区别是什么? 1:数组是定长,list是自动增长.2:数组效率高,list效率低.总结:数组牺牲功能增加效率,list牺牲效率增加功能. http://bbs.cs ...

  5. mysql的replication(主从同步)总结

    很好的文章,对mysql的主从架构有深入理解. mysql主从同步,从master同步数据到slave慢的情况下,是不是可以改成多线程处理加快同步速度? 参考文章如下: MySQL Replicati ...

  6. UITableView初始

    近期在自学IOS,看了黑马提供的视频,讲的很好.在此做些笔记,以供以后查阅.注明了知识来源应该不算侵权吧. 一 UITableView 1,数据展示的条件 1⃣️ UITableView的所有数据都是 ...

  7. Hbase常用操作

    下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如 名称 命令表达式 创建表 create '表名称', '列名称1','列名称2','列名称N' ...

  8. sqlserver2012 评估期已过问题处理

    由于之前安装sqlserver2012忘记输入序列号,现在出现评估期已过的问题,网上忙活半天,才解决,发现网上叙述都很凌乱,而且只有大意,新手很难操作,所以把我操作的过程分享给大家 1 开始菜单找到s ...

  9. UNIX时间戳及日期的转换与计算

    UNIX时间戳是保存日期和时间的一种紧凑简洁的方法,是大多数UNIX系统中保存当前日期和时间的一种方法,也是在大多数计算机语言中表示日期和时间的一种标准格式.以32位整数表示格林威治标准时间,例如,使 ...

  10. 文件头 MAGE_FILE_HEADER

    IMAGE_FILE_HEADER这个结构的定义如下: typedef struct _IMAGE_FILE_HEADER { 00h WORD Machine; //运行平台 02h WORD Nu ...