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 ...
随机推荐
- SQL server 提取字符中第一次和最后一次出现的数字
CREATE FUNCTION [dbo].[StringExtractNumber(FirstOrLast)](@address nvarchar(max),@firstOrLast INT) re ...
- ueditor UEditor的setContent的时候报错
今天在使用UEditor的setContent的时候报错,报错代码如下 TypeError: me.body is undefined 或 Uncaught TypeError: Cannot set ...
- Mybatis环境搭建中的案例分析 及 如果自己编写DAO接口的实现类
Mybatis环境搭建中的案例分析public static void main (String[] args) throws Exception { //读配置文件 //第一个: 使用类加载器,只能 ...
- 【转】Linux将composer的bin目录放到PATH环境变量中
将composer的bin目录放到PATH环境变量中 使用composer global config bin-dir --absolute查看composer的bin目录 输出类似 Changed ...
- 线段树 SP1043 GSS1 - Can you answer these queries I
SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...
- 记录下自己安装cuda以及cudnn
之前已经装过一次了,不过没有做记录,现在又要翻一堆博客安装,长点记性,自己记录下. 环境 ubuntu16.04 python2.7 商家送过来时候已经装好了显卡驱动,所以省去了一大麻烦. 剩下的就是 ...
- liunx 内置mail 发送邮件
邮件配置文件/etc/mail.rc [root@ ~]# vim /etc/mail.rc #添加.修改如下内容 .com .com .com set smtp-auth-password=xxx ...
- sharepoint_study_7
描述:sharepoint网站上部署WebPart出错后,如何删除错误的WebPart?如何恢复原页面? 解决:到管理中心去将该解决方案收回并删除,可以恢复原页面,但是错误的webpart信息会保留, ...
- Django之auth模块(用户认证)登陆组件
auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...
- 网络流EdmondsKarp算法模板理解
先推荐一个讲网络流的博客,我的网络流知识均吸收于此 传送门 EdmondsKarp算法基本思想:从起点到终点进行bfs,只要存在路,说明存在增广路径,则取这部分路 权值最小的一部分,即为增广路径( ...