05-图1. List Components (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered from 0 to N-1. While searching, assume that we always start from the vertex with the smallest index, and visit its adjacent vertices in ascending order of their indices.

Input Specification:

Each input file contains one test case. For each case, the first line gives two integers N (0<N<=10) and E, which are the number of vertices and the number of edges, respectively. Then E lines follow, each described an edge by giving the two ends. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in each line a connected component in the format "{ v1 v2 ... vk }". First print the result obtained by DFS, then by BFS.

Sample Input:

8 6
0 7
0 1
2 0
4 1
2 4
3 5

Sample Output:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }

提交代码

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<string>
using namespace std;
bool map[][];
bool vis[];
queue<int> q;
int n,e;
void DFS(int a){
q.push(a);
vis[a]=true;
int i;
for(i=;i<n;i++){
if(!vis[i]&&map[a][i]){
DFS(i);
}
}
}
void BFS(int a){
queue<int> qq;
qq.push(a);
q.push(a);
vis[a]=true;
int i;
while(!qq.empty()){
a=qq.front();
qq.pop();
for(i=;i<n;i++){
if(!vis[i]&&map[a][i]){
vis[i]=true;
q.push(i);
qq.push(i);
}
}
}
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int a,b;
int i;
scanf("%d %d",&n,&e);
memset(map,false,sizeof(map));
memset(vis,false,sizeof(vis));
for(i=;i<e;i++){
scanf("%d %d",&a,&b);
map[a][b]=map[b][a]=true;
}
for(i=;i<n;i++){
if(!vis[i]){
DFS(i);
if(!q.empty()){
printf("{");//{ 0 1 4 2 7 }
while(!q.empty()){
printf(" %d",q.front());
q.pop();
}
printf(" }\n");
}
}
}
memset(vis,false,sizeof(vis));
for(i=;i<n;i++){
if(!vis[i]){
BFS(i);
if(!q.empty()){
printf("{");//{ 0 1 4 2 7 }
while(!q.empty()){
printf(" %d",q.front());
q.pop();
}
printf(" }\n");
}
}
}
return ;
}

pat05-图1. List Components (25)的更多相关文章

  1. 05-图1. List Components (25)

    05-图1. List Components (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue For a ...

  2. 【(图) 旅游规划 (25 分)】【Dijkstra算法】

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...

  3. FusionChart实现柱状图、饼状图的动态数据显示 附Demo

    最近做的项目中需要用饼状图显示——'问卷调查'的统计结果(之前用过FusionChart做过柱状图的数据展示,那还是两年前的事了),在网上查了下FusionChart实现饼状图显示方面的资料,却发现资 ...

  4. android 股票K线图

    现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有K线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下. 下面是做出来的效果图: 这个 界面 ...

  5. scrum立会报告+燃尽图(第二周第二次)

    此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2247 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...

  6. FusionChart实现柱状图、饼状图的动态数据显示

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Vue2 轮播图组件 slide组件

    Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...

  8. Scrum立会报告+燃尽图(十月三十日总第二十一次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2290 项目地址:https://git.coding.net/zhang ...

  9. Scrum立会报告+燃尽图(十月二十九日总第二十次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 项目地址:https://git.coding.net/zhang ...

随机推荐

  1. C++: I/O流详解

    一.输入流操作 1.read 无格式输入指定字节数 istream& read ( char* pch, int nCount ); istream& read ( unsigned ...

  2. 数据结构42:n个结点构造多少种树

    本节要讨论的是当给定 n(n>=0)个结点时,可以构建多少种形态不同的树. 如果两棵树中各个结点的位置都一一对应,可以说这两棵树相似.如果两棵树不仅相似,而且对应结点上的数据也相同,就可以说这两 ...

  3. 【模板】割点(割顶) Tarjan

    题目背景 割点 题目描述 给出一个nnn个点,mmm条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,mn,mn,m 下面mmm行每行输入x,yx,yx,y表示xxx到yyy有一条边 ...

  4. apache 日志分割

    Window apache 全局设置日志分割   apache  [ httpd.conf ] 配置文件 开启日志模块:LoadModule log_config_module modules/mod ...

  5. equals和== 区别

    转载:https://zhidao.baidu.com/question/61622858.html ==是一个比较运算符,基本数据类型比较的是值,引用数据类型比较的是地址值. (比较地址值即是指是否 ...

  6. centos7-网络与防火墙常用命令

    1.网络配置 vi /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO="static" IPADDR=192.168.163 ...

  7. eclipse+pydev 安装和配置过程

    安装 PyDev 在安装 PyDev 之前,要保证您已经安装了 Java 1.4 或更高版本.Eclipse 以及 Python.接下来,开始安装 PyDev 插件. 启动 Eclipse,利用 Ec ...

  8. vue 父组件调用子组件的函数

    子组件 <template> <div> child </div> </template> <script> export default ...

  9. thinkPHP5.0联表查询和统计文章的图片(栏目文章)数量

    public function index(){ //获取内容列表信息 $res = db('article')->alias('a') ->join('category b', 'b.i ...

  10. 关于处理百万级大批量数据的mysql运行几个重要点

    处理大批量百万级的数据几点重要知识点: 一:设置php运行的内存配置 ini_set("memory_limit","1200M"); 在php.ini中有如下 ...