数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

Time Limit: 1000MS Memory limit: 65536K

题目描述

给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列。(同一个结点的同层邻接点,节点编号小的优先遍历)

输入

输入第一行为整数n(0< n <100),表示数据的组数。
对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),表示有m条边,k个顶点,t为遍历的起始顶点。
下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。

输出

输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示BFS的遍历结果。

示例输入

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

示例输出

0 3 4 2 5 1

提示

以邻接矩阵作为存储结构。
 
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue> using namespace std; int map[102][102];
int vis[102];
int n;
queue<int>q;
int a[102], e=0; void bfs( int dd )
{
q.push(dd);
vis[dd]=1;
e=0;
int ff;
int j; while(!q.empty())
{
ff=q.front();
a[e++]=ff;
q.pop(); for(j=0; j<n; j++)
{
if(vis[j]==0 && map[ff][j]==1 )
{
q.push(j);
vis[j]=1;
}
}
}
} int main()
{
int t;
cin>>t;
int m, s;
int i, j;
int u, v; while(t--)
{
cin>>n>>m>>s;
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis)); for(i=0; i<m; i++)
{
cin>>u>>v;
map[u][v]=1;
map[v][u]=1;
}
bfs(s); for(j=0; j<e; j++)
{
if(j==0)
cout<<a[0];
else
cout<<" "<<a[j];
}
cout<<endl;
}
return 0;
}

数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)的更多相关文章

  1. SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...

  2. SDUT-2124_基于邻接矩阵的广度优先搜索遍历

    数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个无向连通图 ...

  3. SDUT2141数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<cstdio> #include& ...

  4. 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 (SDUT 2141)

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

  5. 基于邻接矩阵的广度优先搜索遍历(BFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<stdio.h> #incl ...

  6. C语言数据结构与算法之深度、广度优先搜索

    一.深度优先搜索(Depth-First-Search 简称:DFS) 1.1 遍历过程: (1)从图中某个顶点v出发,访问v. (2)找出刚才第一个被顶点访问的邻接点.访问该顶点.以这个顶点为新的顶 ...

  7. Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)

    Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  8. 基于邻接矩阵的深度优先搜索(DFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2107&cid=1186 #include<stdio.h> #incl ...

  9. CF1272E. Nearest Opposite Parity 题解 广度优先搜索

    题目链接:http://codeforces.com/contest/1272/problem/E 题目大意: 有一个长度为n的数组 \(a\) ,数组坐标从 \(1\) 到 \(n\) . 假设你现 ...

随机推荐

  1. spring和mybatis整合配置文件

    查看所有springmvc  spring  mybatis配置文件见下链接: https://my.oschina.net/sherwayne/blog/262616 <?xml versio ...

  2. 【网摘】sql 语句修改字段名称以及字段类型

    网上摘抄,备份使用: 修改字段名: 下例将表 customers 中的列 contact title 重命名为 title. EXEC sp_rename 'customers.[contact ti ...

  3. android test控件

    1.Plain Text 输入文本框 <EditText android:id="@+id/editText" android:layout_width="wrap ...

  4. CODEVS 1245 最小的N个和 堆+排序

    原题链接 http://codevs.cn/problem/1245/ 题目描述 Description 有两个长度为 N 的序列 A 和 B,在 A 和 B 中各任取一个数可以得到 N^2 个和,求 ...

  5. javaWeb_Request对象

    首先说一下Http协议 一.Http协议的概念及作用 1.什么是HTTP协议? (HTTP,HyperText Transfer Protocol)超文本传输协议, 是互联网上应用最为广泛的一种网络协 ...

  6. Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决

    如果在Maven构建时出现: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable d ...

  7. Jquery表单序列化和json操作

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. ORACLE 内部原理

    http://www.ohsdba.cn/index.php?m=Article&a=index&id=46 内部原理 2016-05-04• 如何使用BBED 2016-04-16• ...

  9. 【hibernate】报错:org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

    报错如下: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a ...

  10. [置顶] MySQL -- 创建函数(Function

    目标 如何在MySQL数据库中创建函数(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,参数是可选的 RETU ...