国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

數據結構實驗之圖論二:基于鄰接表的廣度優先搜索遍歷

2019-11-08 19:47:29
字體:
來源:轉載
供稿:網友

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

16 7 00 30 41 41 52 32 43 5

Example Output

0 3 4 2 5 1
 
#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxn 100typedef struct node{    int data;    struct node * next;}map;map * tu[maxn];int book[maxn];int queue[maxn];int num[maxn];int top,front,rear;void sort(int k){    int i,tmp;    map * p, *q;    for(i=0;i<k;i++)    {        p=tu[i];        if(!p||!p->next)            continue;        for(;p;p=p->next)        {            q=p->next;            for(;q;q=q->next)            {                if(p->data > q->data)                {                    tmp=p->data;                    p->data=q->data;                    q->data=tmp;                }            }        }    }}void bfs(int x){    int i,j;    map * p;    book[x]=1;    rear++;    queue[rear]=x;    while(front!=rear)    {        front++;        i=queue[front];        num[++top]=i;        p=tu[i];        while(p)        {            j=p->data;            if(!book[j])            {                book[j]=1;                rear++;                queue[rear]=j;            }            p=p->next;        }    }}int main(){    int i,n,k,m,t,u,v;    map *p;    scanf("%d",&n);    while(n--)    {        top=-1;        front=rear=0;        memset(book,0,sizeof(book));        scanf("%d%d%d",&k,&m,&t);        for(i=0;i<k;i++)        {            tu[i]=NULL;        }        while(m--)        {            scanf("%d%d",&u,&v);            if(!tu[u])            {                p=(map *)malloc(sizeof(tu));                p->data=v;                p->next=NULL;                tu[u]=p;            }            else            {                p=(map *)malloc(sizeof(tu));                p->data=v;                p->next=tu[u]->next;                tu[u]->next=p;            }            if(!tu[v])            {                p=(map *)malloc(sizeof(tu));                p->data=u;                p->next=NULL;                tu[v]=p;            }            else            {                p=(map *)malloc(sizeof(tu));                p->data=u;                p->next=tu[v]->next;                tu[v]->next=p;            }        }        sort(k);        bfs(t);        for(i=0;i<=top;i++)        {            if(i!=top)            {                printf("%d ",num[i]);            }            else            {                printf("%d/n",num[i]);            }        }    }    return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浑源县| 安图县| 永春县| 黄冈市| 内黄县| 蛟河市| 云阳县| 南宁市| 融水| 盖州市| 大石桥市| 大埔区| 平塘县| 北碚区| 漯河市| 烟台市| 邢台县| 乐平市| 府谷县| 阜阳市| 保亭| 温泉县| 赞皇县| 白城市| 广德县| 清新县| 云南省| 瑞昌市| 泊头市| 探索| 彭阳县| 枣庄市| 临安市| 广西| 红安县| 尼木县| 宕昌县| 平泉县| 文安县| 德安县| 合阳县|