题目描述
给定一个无向连通图,顶点编号从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顶点的无向边。
对于每组数据,第一行是三个整数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
1 6 7 0 0 3 0 4 1 4 1 5 2 3 2 4 3 5
示例输出
0 3 4 2 5 1
0 3 4 2 5 1
提示
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int vis[1000];//vis数组标记顶点是否已访问
struct hh
{
int data;//顶点编号
struct hh *next;//下一个顶点
};
struct h
{
int n;//边数
int e;//顶点数
struct hh Point[110];//顶点
};
struct h *creat(struct h *g)
{
struct hh *p,*q1,*q2;
int i,v,u;
//初始化链表顶点
for(i=0;i<g->n;i++)
{
g->Point[i].data=i;
g->Point[i].next=NULL;
}
//将有连接的两个顶点连接在一起
for(i=0;i<g->e;i++)
{
scanf("%d%d",&u,&v);
//将u连接到v上
p=(struct hh *)malloc(sizeof(struct hh));
p->data=u;
q1=&g->Point[v];
while(q1->next!=NULL)
q1=q1->next;
p->next=q1->next;
q1->next=p;
q1=p;
//将v连接到u上
p=(struct hh *)malloc(sizeof(struct hh));
p->data=v;
q2=&g->Point[u];
while(q2->next!=NULL)
q2=q2->next;
p->next=q2->next;
q2->next=p;
q2=p;
}
return g;
};
void BFS(struct h *g,int v)
{
struct hh *p;
int queue[1000],front=0,rear=0;
int w;
//访问输出第一个值并标记已访问
printf("%d",v);
vis[v]=1;
//已访问的顶点放入队列
rear=(rear+1)%1000;
queue[rear]=v;
while(front!=rear)
{
//已访问的顶点出队
front=(front+1)%1000;
w=queue[front];
//找到和已访问的顶点连接的顶点
p=g->Point[w].next;
//挨个访问和已访问顶点连接的顶点
while(p!=NULL)
{
if(vis[p->data]==0)
{
//访问未访问的顶点
printf(" %d",p->data);
vis[p->data]=1;
//顶点入队
rear=(rear+1)%1000;
queue[rear]=p->data;
}
p=p->next;
}
}
}
int main()
{
int m,i,t;
struct h *g;
g=(struct h *)malloc(sizeof(struct h));
scanf("%d",&m);
for(i=0;i<m;i++)
{
//输入边数、顶点数和遍历起始顶点
scanf("%d%d%d",&g->n,&g->e,&t);
//初始化vis数组
memset(vis,0,sizeof(vis));
//调用creat函数创建图
g=creat(g);
//调用广度优先搜索
BFS(g,t);
printf("\n");
}
return 0;
}
#include<stdio.h> #include<stdlib.h> #include<string.h> int vis[1000];//vis数组标记顶点是否已访问 struct hh { int data;//顶点编号 struct hh *next;//下一个顶点 }; struct h { int n;//边数 int e;//顶点数 struct hh Point[110];//顶点 }; struct h *creat(struct h *g) { struct hh *p,*q1,*q2; int i,v,u; //初始化链表顶点 for(i=0;i<g->n;i++) { g->Point[i].data=i; g->Point[i].next=NULL; } //将有连接的两个顶点连接在一起 for(i=0;i<g->e;i++) { scanf("%d%d",&u,&v); //将u连接到v上 p=(struct hh *)malloc(sizeof(struct hh)); p->data=u; q1=&g->Point[v]; while(q1->next!=NULL) q1=q1->next; p->next=q1->next; q1->next=p; q1=p; //将v连接到u上 p=(struct hh *)malloc(sizeof(struct hh)); p->data=v; q2=&g->Point[u]; while(q2->next!=NULL) q2=q2->next; p->next=q2->next; q2->next=p; q2=p; } return g; }; void BFS(struct h *g,int v) { struct hh *p; int queue[1000],front=0,rear=0; int w; //访问输出第一个值并标记已访问 printf("%d",v); vis[v]=1; //已访问的顶点放入队列 rear=(rear+1)%1000; queue[rear]=v; while(front!=rear) { //已访问的顶点出队 front=(front+1)%1000; w=queue[front]; //找到和已访问的顶点连接的顶点 p=g->Point[w].next; //挨个访问和已访问顶点连接的顶点 while(p!=NULL) { if(vis[p->data]==0) { //访问未访问的顶点 printf(" %d",p->data); vis[p->data]=1; //顶点入队 rear=(rear+1)%1000; queue[rear]=p->data; } p=p->next; } } } int main() { int m,i,t; struct h *g; g=(struct h *)malloc(sizeof(struct h)); scanf("%d",&m); for(i=0;i<m;i++) { //输入边数、顶点数和遍历起始顶点 scanf("%d%d%d",&g->n,&g->e,&t); //初始化vis数组 memset(vis,0,sizeof(vis)); //调用creat函数创建图 g=creat(g); //调用广度优先搜索 BFS(g,t); printf("\n"); } return 0; }