SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
数据结构实验之图论二:基于邻接表的广度优先搜索遍历
Problem Description
Input
对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),表示有m条边,k个顶点,t为遍历的起始顶点。
下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。
Output
Example Input
- 1
- 6 7 0
- 0 3
- 0 4
- 1 4
- 1 5
- 2 3
- 2 4
- 3 5
Example Output
- 0 3 4 2 5 1
Hint
DQE:
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #include <stack>
- using namespace std;
- #define MVN 110
- typedef struct ArcNode
- {
- int adj;
- ArcNode *next;
- char *info;
- }AN; //弧结点
- typedef struct VNode
- {
- int x;
- AN *first;
- }VN; //顶点节点
- typedef struct ALGraph
- {
- VN vex[MVN];
- int vexn,arcn;
- int s;
- }ALG; //基于邻接表的图
- void creat(ALG &G)
- {
- int i,j,k;
- for(k=;k<G.vexn;k++)
- G.vex[k].first=NULL;
- for(k=;k<G.arcn;k++)
- {
- scanf("%d %d",&i,&j);
- AN *ia=new AN,*ja=new AN;
- ia->adj=j;
- ja->adj=i;
- ia->next=G.vex[i].first;
- ja->next=G.vex[j].first;
- G.vex[i].first=ia;
- G.vex[j].first=ja;
- }
- }
- void BFS(ALG &G)
- {
- int i;
- queue <int> Q;
- stack <int> S;
- bool f[MVN]={false};
- Q.push(G.s);
- while(!Q.empty())
- {
- i=Q.front();Q.pop();
- if(!f[i])
- {
- AN *p=G.vex[i].first;
- while(p)
- {
- S.push(p->adj);
- p=p->next;
- }
- //利用栈纠正临接点顺序
- while(!S.empty())
- {
- Q.push(S.top());S.pop();
- }
- if(i==G.s)
- printf("%d",i);
- else
- printf(" %d",i);
- f[i]=true;
- }
- }
- printf("\n");
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- while(t--)
- {
- ALG G;
- scanf("%d %d %d",&G.vexn,&G.arcn,&G.s);
- creat(G);
- BFS(G);
- }
- return ;
- }
- /***************************************************
- User name: ***
- Result: Accepted
- Take time: 0ms
- Take Memory: 156KB
- Submit time: 2016-11-18 19:51:04
- ****************************************************/
SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历的更多相关文章
- SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...
- 基于邻接表的广度优先搜索遍历(bfs)
题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 #include<stdio.h> #incl ...
- SDUT OJ 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
- SDUT 3363 数据结构实验之图论七:驴友计划
数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...
- SDUT 3362 数据结构实验之图论六:村村通公路
数据结构实验之图论六:村村通公路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 当前农村公 ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- SDUT OJ 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)
数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
随机推荐
- 《Javascript高级程序设计》阅读记录(二):第四章
这个系列之前文字地址:http://www.cnblogs.com/qixinbo/p/6984374.html 这个系列,我会把阅读<Javascript高级程序设计>之后,感觉讲的比较 ...
- BZOJ4808: 马
BZOJ4808: 马 https://lydsy.com/JudgeOnline/problem.php?id=4808 分析: 黑白染色,求二分图最大匹配即可. 代码: #include < ...
- 用javascript实现base64编码器以及图片的base64编码
前面的话 base-64作为常见的编码函数,在基本认证.摘要认证以及一些HTTP扩展中得到了大量应用.在前端领域,也常常把图片转换为base-64编码在网络中传输.本文将详细介绍base64的原理及用 ...
- 笔记:加 ly 不一定是副词
笔记:加 ly 不一定是副词 加 ly 变副词,但有些单词以 ly 结尾,长得像副词,却是形容词. costly = cost + ly a costly item. 一件昂贵的物品. lovely ...
- DropShadowEffect导致下拉框控件抖动
<!--<Border.Effect> <DropShadowEffect Direction="180" BlurRadius="1" ...
- commandLink/commandButton/ajax backing bean action/listener method not invoked (转)
Whenever an UICommand component fails to invoke the associated action method or an UIInputelement fa ...
- CodeForces 620E:New Year Tree(dfs序+线段树)
E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...
- bootstrap简单的签收页面
http://aqvatarius.com/themes/atlant/html/ui-icons.html <%@ Page Language="C#" AutoEvent ...
- IIS:配置参数
ylbtech-IIS:配置参数 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 ...
- javaScript之NodeList
NodeList对象 是DOM操作取出的集合(实际上是基于DOM结构动态查询的结果),用来保存一组有序的节点,可以通过位置来访问这些节点,它并不是array的实例. Nodelist最大的特点就是它的 ...