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

来源:互联网 发布:声音模拟软件下载 编辑:程序博客网 时间:2024/06/03 19:11

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

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

输入第一行为整数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顶点的无向边。
Output

输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示BFS的遍历结果。
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

用邻接表存储。
Author

基于连接表的广度优先搜索,首先要建立连接表,并按照节点的大小建立,然后递归调用bfs,并用数组存储起来……

这道题目关键要细心,就和普通链表差不多,一旦没考虑好NULL的问题,就会RE或者不出数据……

#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAXN 121struct node{    int data;    struct node * next;};int n,op,tp;struct node *a[MAXN];int v[MAXN];int q[MAXN];void bfs(int k){    op++;    struct node * p =(struct node *)malloc(sizeof(struct node));    p = a[k]->next;    while(p)    {        if(v[p->data]==0)        {            v[p->data] = 1;            q[tp++] = p->data;        }        p = p->next;    }    if(op<=tp)        bfs(q[op]);}int main(){    int t, x, y, l, m;    scanf("%d", &t);    while(t--)    {        scanf("%d %d %d", &n, &m, &l);        op = tp  = 0;        memset(v, 0, sizeof(v));//初始化        for(int i =0;i<n;i++)//清空            a[i] = NULL;        while(m--)        {            scanf("%d %d", &x, &y);            //x--y的路径            if(a[x]==NULL)            {                struct node * p = (struct node *)malloc(sizeof(struct node));                p->data = x;                struct node * q = (struct node *)malloc(sizeof(struct node));                q->data = y;                q->next = NULL;                a[x] = p;                p->next = q;            }            else            {               struct node * p = (struct node *)malloc(sizeof(struct node));               p =a[x]->next;               struct node * q = (struct node *)malloc(sizeof(struct node));               q->data = y;                 if(p == NULL)                {                    q->next = a[x]->next;                    a[x]->next = q;                }               while(p!=NULL)               {                   if(p->next)                   {                       if(p->data<=q->data&&q->data<p->next->data)                       {                           q->next =p->next;                           p->next = q;                           break;                       }                   }                  if(p->next==NULL)                  {                   if(p->data<=q->data)                   {                       q->next=p->next;                       p->next =q;                       break;                   }                  }                  p= p->next;               }            }        //y--x的路径            if(a[y]==NULL)            {                struct node * p = (struct node *)malloc(sizeof(struct node));                p->data = y;                struct node * q = (struct node *)malloc(sizeof(struct node));                q->data = x;                q->next = NULL;                a[y] = p;                p->next = q;            }            else            {               struct node * p = (struct node *)malloc(sizeof(struct node));               p =a[y]->next;               struct node * q = (struct node *)malloc(sizeof(struct node));               q->data = x;               if(p == NULL)                {                    q->next = a[y]->next;                    a[y]->next = q;                }               while(p!=NULL)               {                   if(p->next)                   {                       if(p->data<=q->data&&q->data<p->next->data)                       {                           q->next =p->next;                           p->next = q;                           break;                       }                   }                  if(p->next==NULL)                  {                   if(p->data<=q->data)                   {                       q->next=p->next;                       p->next =q;                       break;                   }                  }                  p= p->next;               }            }        }        //bfs            v[l] = 1;            q[tp++] = l;            bfs(l);            //输出            for(int i=0;i<tp;i++)            {                printf("%d%c", q[i], tp-1==i?'\n':' ');            }        }    return 0;}
阅读全文
0 0
原创粉丝点击