数据结构实验之图论二:基于邻接表的广度优先搜索遍历

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 <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>; using namespace std; struct node
{
int data;
int next;
}a[6000]; int cnt;
int head[101];
void Insert_edge(int u, int v)
{
a[cnt].data=u;
a[cnt].next=head[v];
head[v]=cnt++;
} int list[101],e;
void bfs(int n, int s) //n个点, s是起点
{
queue<int>q;
bool vis[101];
memset(vis, false, sizeof(vis));
q.push(s);
vis[s]=true;
int i, dd; //当前队首元素
int ff; while(!q.empty())
{
dd=q.front();
q.pop();
list[e++]=dd;
int w[101], p=0;
//queue<int>p;
for(i=head[dd]; i!=-1; i=a[i].next )
{
ff=a[i].data;
if(vis[ff]==false)
{
//p.push(ff);
w[p++]=ff;
vis[ff]=true;
}
}
sort(w, w+p); //排序 为了保证大小顺序
for(i=0; i<p; i++)
{
q.push(w[i]); //遍历结果加入队列
}
}
while(!q.empty())
{
ff=q.front();
q.pop();
list[e++]=ff;
}
} int main()
{
int t;
cin>>t;
int i, j;
int n, m, u, v;
int start; while(t--)
{
cin>>n>>m>>start;
cnt=0;
memset(head, -1, sizeof(head));
for(i=0; i<m; i++)
{
cin>>u>>v;
Insert_edge(u, v);
Insert_edge(v, u); //无向图插入边
}
bfs(n, start);
for(i=0; i<e; i++)
{
if(i==0)
cout<<list[i];
else
cout<<" "<<list[i];
}
cout<<endl;
}
return 0;
}

数据结构之 图论---bfs(邻接表)的更多相关文章

  1. 《数据结构》C++代码 邻接表与邻接矩阵

    上一篇“BFS与DFS”写完,突然意识到这个可能偏离了“数据结构”的主题,所以回来介绍一下图的存储:邻接表和邻接矩阵. 存图有两种方式,邻接矩阵严格说就是一个bool型的二维数组,map[i][j]表 ...

  2. bfs 邻接表(需要优化 可能会RE *【模板】)

    //---基于邻接表的bfs #include <stdio.h> #include <string.h> #include <iostream> #include ...

  3. I - 乓 (BFS+邻接表)

    USTC campus network is a huge network. There is a bi-directional link between every pair of computer ...

  4. 从0开始 图论学习 邻接表 STL vector

    邻接表表示 用vector实现 writer:pprp 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn ...

  5. c_数据结构_图_邻接表

    课程设计------邻接表 图的遍历实现课程设计:https://files.cnblogs.com/files/Vera-y/图的遍历_课程设计.zip #include<stdio.h> ...

  6. bfs 邻接表

    #include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int date; st ...

  7. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历

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

  8. ACM/ICPC 之 数据结构-邻接表+BFS(TSH OJ-无线广播Broadcast)

    这道题中若能够构成互不干扰的区域,其构成的图其实就是汉密尔顿路(Hamilton road),因此如果能够观察出来可以直接转化为汉密尔顿路的存在性证明,即便不能观察,我相信ACMer也能转化为BFS问 ...

  9. 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS

    图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...

随机推荐

  1. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 407  Solved: 325[S ...

  2. 染色(bzoj 2243)

    Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ ...

  3. NOIP 前夕 模板整理

    归并排序: #include<iostream> #include<cstdio> #include<cstring> using namespace std; ] ...

  4. 转 Linux文件管理

    Linux文件管理 http://www.cnblogs.com/vamei/archive/2012/09/09/2676792.html 作者:Vamei 出处:http://www.cnblog ...

  5. Codeforces 667D World Tour【最短路+枚举】

    垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...

  6. HDU - 5572 An Easy Physics Problem (计算几何模板)

    [题目概述] On an infinite smooth table, there's a big round fixed cylinder and a little ball whose volum ...

  7. 9.Java web—JSP内置对象

    容器内置了9大对象,这些对象在jsp页无需实例化,可以直接使用. 分别为request. response .session. application .out. pageContext .confi ...

  8. Jetty插件实现热部署(开发时修改文件自动重启Jetty)

    在pom.xml文件中配置Jetty插件的参数:scanIntervalSeconds <plugin> <groupId>org.mortbay.jetty</grou ...

  9. golang几种post请求方式

    get请求 get请求可以直接http.Get方法,非常简单. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 func httpGet() {     resp, err := h ...

  10. Python爬虫简单实现之Q乐园图片下载

    根据需求写代码实现.然而跟我并没有什么关系,我只是打开电脑望着屏幕想着去干点什么,于是有了这个所谓的“需求”. 终于,我发现了Q乐园——到底是我老了还是我小了,这是什么神奇的网站,没听过啊,就是下面酱 ...