pat05-图1. List Components (25)
05-图1. List Components (25)
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)的更多相关文章
- 05-图1. List Components (25)
05-图1. List Components (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue For a ...
- 【(图) 旅游规划 (25 分)】【Dijkstra算法】
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- FusionChart实现柱状图、饼状图的动态数据显示 附Demo
最近做的项目中需要用饼状图显示——'问卷调查'的统计结果(之前用过FusionChart做过柱状图的数据展示,那还是两年前的事了),在网上查了下FusionChart实现饼状图显示方面的资料,却发现资 ...
- android 股票K线图
现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有K线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下. 下面是做出来的效果图: 这个 界面 ...
- scrum立会报告+燃尽图(第二周第二次)
此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2247 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...
- FusionChart实现柱状图、饼状图的动态数据显示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Vue2 轮播图组件 slide组件
Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...
- Scrum立会报告+燃尽图(十月三十日总第二十一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2290 项目地址:https://git.coding.net/zhang ...
- Scrum立会报告+燃尽图(十月二十九日总第二十次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 项目地址:https://git.coding.net/zhang ...
随机推荐
- 校对双层PDF中的隐藏文本
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2012.06.11 目录一.背景二.能够校对的PDF需要满足的条件三.校对工具的选择四.校对过程五.延伸讨论 事先声明:本文 ...
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的 ...
- cuda编程知识普及
本帖经过多方整理,大多来自各路书籍<GPGPU编程技术><cuda高性能> 1 grid 和 block都可以用三元向量来表示: grid的数组元素是block blo ...
- html之表单和简单CSS
一.==表单== 1. form表单本身 <form name="myform" action="#" method="get"> ...
- Appium教程——Desired Capabilities 详解(转自TesterHome)
Desired Capabilities在启动session的时候是必须提供的. Desired Capabilities本质上是key value的对象,它告诉appium server这样一些事情 ...
- 【Cracking the Code Interview(5th edition)】一、数组与字符串(C++)
1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.不允许使用额外的数据结构. 解答:这里假定字符集为ASCII码,可以与面试官沟通确认字符串使用的字符集.由于字符集是有限的,建立一个数组模拟 ...
- [JavaScript]如何实现一个JS脚本能在browser和NodeJs里都是用
下面是一个Common的Solution 'use strict'; // Universal Module Definition (UMD) to support AMD, CommonJS/Nod ...
- C语言中Extern用法
extern用在变量或函数的声明前,用来说明“此变量/函数是在别处定义的,要在此处引用”. extern修饰变量的声明. 举例:若a.c中需引用b.c中的变量int v,可以在a.c中声明extern ...
- php __CLASS__、get_class()与get_called_class()的区别
__CLASS__获取当前的类名, get_class()与上面一样,都是获取当前的类名 get_called_class()获取当前主调类的类名 当涉及到继承时,在方法中使用类名.直接贴图了 MVC ...
- 08-----pymysql模块使用
pymysql的下载和使用 exctue() 之sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetchall 一.pytmysql的下载和使用 ...