标签:des style blog io ar os sp for 数据
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遍历序列)
标签:des style blog io ar os sp for 数据
原文地址:http://www.cnblogs.com/yspworld/p/4103999.html