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

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

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的遍历结果。

Sample Input

1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5

Sample Output

0 3 4 2 5 1

Hint

用邻接表存储。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    int data;
    struct node *next;

}*a[1001], *p, *q;

int flag[10001], stack[10001];
struct node *num[101];  //为什么num它总是自己会清NULL,明明是全局变量)

void build(int u, int v)
{
    // struct node *x;

    p = (struct node *)malloc(sizeof(struct node));
    p-> data = v;
    if(num[u] == NULL)
    {
        p-> next = NULL;
        num[u] = p;
    }

    else
    {
        p-> next = num[u]-> next;
        num[u]-> next = p;
    }

    q = (struct node *)malloc(sizeof(struct node));
    q-> data = u;
    if(num[v] == NULL)
    {
        q-> next = NULL;
        num[v] = q;
    }

    else
    {
        q-> next = num[v]-> next;
        num[v]-> next = q;
    }

}

int search(struct node *head, int i)
{
    while(head)
    {
       if(head -> data == i)
           return 1;
        else
            head = head -> next;
    }
    return 0;
}

int main(void)
{
    int n, i, k, m, t, u, v;

    scanf("%d", &n);
    while(n--)
    {
        scanf("%d %d %d", &k, &m, &t);

        memset(flag, 0, sizeof(flag));
        memset(stack, 0, sizeof(stack));

        for(i = 0; i < m; i++)
        {
            scanf("%d %d", &u, &v);
            build(u, v);
        }

        int l = 0, r = 0;
        flag[t] = 1;
        stack[r++] = t;

        while(l < r)
        {
            for(i = 0; i < k; i++)
            {
                if(search(num[stack[l]], i) && flag[i] == 0)
                {
                    stack[r++] = i;
                    flag[i] = 1;
                }
            }

            printf("%d%c", stack[l], l == k - 1?'\n':' ');
            l++;
        }
    }

    return 0;
}


/*

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    int data;
    struct node *next;

}*a[1001], *p, *q;

int flag[10001], stack[10001];

void build(struct node *num[], int u, int v)
{
    // struct node *x;

    p = (struct node *)malloc(sizeof(struct node));
    p-> data = v;
    if(num[u] == NULL)
    {
        p-> next = NULL;
        num[u] = p;
    }

    else
    {
        p-> next = num[u]-> next;
        num[u]-> next = p;
    }

    q = (struct node *)malloc(sizeof(struct node));
    q-> data = u;
    if(num[v] == NULL)
    {
        q-> next = NULL;
        num[v] = q;
    }

    else
    {
        q-> next = num[v]-> next;
        num[v]-> next = q;
    }

}

int search(struct node *head, int i)
{
    while(head)
    {
       if(head -> data == i)
           return 1;
        else
            head = head -> next;
    }
    return 0;
}

int main(void)
{
    int n, i, k, m, t, u, v;

    scanf("%d", &n);
    while(n--)
    {
        scanf("%d %d %d", &k, &m, &t);
        struct node *num[101] = {NULL};;
        memset(flag, 0, sizeof(flag));
        memset(stack, 0, sizeof(stack));

        for(i = 0; i < m; i++)
        {
            scanf("%d %d", &u, &v);
            build(num, u, v);
        }

        int l = 0, r = 0;
        flag[t] = 1;
        stack[r++] = t;

        while(l < r)
        {
            for(i = 0; i < k; i++)
            {
                if(search(num[stack[l]], i) && flag[i] == 0)
                {
                    stack[r++] = i;
                    flag[i] = 1;
                }
            }

            printf("%d%c", stack[l], l == k - 1?'\n':' ');
            l++;
        }
    }

    return 0;
}


/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int stack[110], flag[110];
struct node
{
    int data;
    struct node *next;
};
struct node *num[110];

void creat_list(int u, int v)
{
    struct node *p, *q;
    p = (struct node *)malloc(sizeof(struct node));
    p -> data = v;
    if(num[u] == NULL)
    {
        p -> next = NULL;
        num[u] = p;
    }
    else
    {
        p -> next = num[u] -> next;
        num[u] -> next = p;
    }
    q = (struct node *)malloc(sizeof(struct node));
    q -> data = u;
    if(num[v] == NULL)
    {
        q -> next = NULL;
        num[v] = q;
    }
    else
    {
        q -> next = num[v] -> next;
        num[v] -> next = q;
    }
}

int search(struct node *head, int i)
{
    while(head)
    {
       if(head -> data == i)
           return 1;
        else
            head = head -> next;
    }
    return 0;
}

int main()
{
    int t, n, m, k, u, v, i;
    scanf("%d", &t);
    while(t--)
    {
        memset(flag, 0, sizeof(flag));
        memset(stack, 0, sizeof(stack));
        scanf("%d%d%d", &n, &m, &k);
        for(i = 0; i < m; i++)
        {
            scanf("%d%d", &u, &v);
            creat_list(u, v);
        }
        int l = 0, r = 0;
        flag[k] = 1;
        stack[r++] = k;
        while(l < r)
        {
           // printf("%d\n\n\n\n", n);
            for(i = 0; i < n; i++)
            {
                if(search(num[stack[l]], i) && flag[i] == 0)
                {
                    stack[r++] = i;
                    flag[i] = 1;
                }
            }
            printf("%d%c", stack[l], l == n - 1 ? '\n' : ' ');
            l++;
        }
    }
    return 0;
}
*/


*/

猜你喜欢

转载自blog.csdn.net/Eider1998/article/details/81869273