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 ...
随机推荐
- c#静态方法和非静态方法区别
c#静态方法和非静态方法区别 C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用上会有什么不同呢?让我们来看看最直观的差别:使用了static 修饰符的方法为 ...
- Java实现终止线程池中正在运行的定时任务
源于开发 最近项目中遇到了一个新的需求,就是实现一个可以动态添加定时任务的功能.说到这里,有人可能会说简单啊,使用quartz就好了,简单粗暴.然而quartz框架太重了,小项目根本不好操作啊.当然, ...
- swift 纯代码自定义控件
1.创建自定义控件 import UIKit class CustomView: UIView { var lab:UILabel! var btn:UIButton! /************ 将 ...
- 编译 ambari 2.7.3
官方给的教程比较简单,需要事先安装的工具也是这里列一点,那里列一点.在此记录一下编译要点(在 centos 7 下). 1. 事先需要安装的工具 yum install -y git svn node ...
- C/C++ 遇到0xcccccccc访问冲突
最近一直在纠结这个问题. 最近写代码,总是遇到这个问题,一旦遇到这个问题,以前好使的代码也就不好使了.很费解,上网搜集了下资料.... 这个0xcccccccc是DEBUG模式下,编译器为没有初始化的 ...
- dedecms列表页面随机缩略图调用
如果要利用dedecms制作扁平化主题,大概也能够遇到相似的问题,那就是dedecms的缩略图机制,在没有缩略图的情况下显示单一的默认图片,如果是wordpress可以很方便的定义函数调用随机的缩略图 ...
- vue.js路由嵌套传参
通过配置路由时候按照: path:/user/:username/age/:age 这种就可以把参数传递 接受: $routes.params 接受到的是一个json格式的数据,
- Property 'XXX' not found on type java.lang.String解决方案
一,标签指令错误. 原指令标签: <%@ taglib prefix="c" uri="http://j ava.sun.com/jstl/core" % ...
- Django工程创建
方法一: 1.win+r进入cmd命令窗口: 2.找到Django的安装地址: 3.cmd窗口中利用cd 进入相应的文件夹,再输入命令如下: django-admin.exe startproject ...
- Kibana源码启动报错记录--ENOSPC
执行该命令可解决:echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysc ...