图结构练习——BFSDFS——判断可达性

来源:互联网 发布:java map遍历 编辑:程序博客网 时间:2024/06/18 15:07

think:
1题目注意超时,自己刚开始用的深度优先搜索和有序邻接表,超时
推测如果用邻接矩阵可能会直接runtime error
2反思:根据题意选用适合的数据结构,这个题目建议用广度优先搜索和邻接表

图结构练习——BFSDFS——判断可达性
Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
在古老的魔兽传说中,有两个军团,一个叫天灾,一个叫近卫。在他们所在的地域,有n个隘口,编号为1..n,某些隘口之间是有通道连接的。其中近卫军团在1号隘口,天灾军团在n号隘口。某一天,天灾军团的领袖巫妖王决定派兵攻打近卫军团,天灾军团的部队如此庞大,甚至可以填江过河。但是巫妖王不想付出不必要的代价,他想知道在不修建任何通道的前提下,部队是否可以通过隘口及其相关通道到达近卫军团展开攻击。由于n的值比较大(n<=1000),于是巫妖王找到了擅长编程的你 =_=,请你帮他解决这个问题,否则就把你吃掉变成他的魔法。为了拯救自己,赶紧想办法吧。

Input
输入包含多组,每组格式如下。
第一行包含两个整数n,m(分别代表n个隘口,这些隘口之间有m个通道)。
下面m行每行包含两个整数a,b;表示从a出发有一条通道到达b隘口(注意:通道是单向的)。

Output
如果天灾军团可以不修建任何通道就到达1号隘口,那么输出YES,否则输出NO。

Example Input
2 1
1 2
2 1
2 1

Example Output
NO
YES

Hint
Author
赵利强

以下为accepted代码——广度优先搜索+无序邻接表

#include <stdio.h>#include <string.h>#include <stdlib.h>struct node{    int Data;    struct node *next;}*a[1004], *p;int visit[1004], link[1004];int tp, op, flag;void BFS(int n){    op++;    p = a[n];    while(p != NULL)    {        if(p->Data == 1)        {            flag = 1;            return;        }        if(visit[p->Data] == 0)        {            link[tp++] = p->Data;            visit[p->Data] = 1;        }        p = p->next;    }    if(op <= tp)        BFS(link[op]);}int main(){    int n, m, i, u, v;    while(scanf("%d %d", &n, &m) != EOF)    {        flag = 0;        tp = op = 0;        memset(visit, 0, sizeof(visit));        for(i = 0; i <= n; i++)            a[i] = NULL;        while(m--)        {            scanf("%d %d", &u, &v);            if(a[u] == NULL)            {                p = (struct node *)malloc(sizeof(struct node));                p->Data = v;                p->next = NULL;                a[u] = p;            }            else            {                p = (struct node *)malloc(sizeof(struct node));                p->Data = v;                p->next = a[u]->next;                a[u]->next = p;            }        }        //DFS(n);        link[tp++] = n;        visit[n] = 1;        BFS(n);        if(flag)            printf("YES\n");        else            printf("NO\n");    }    return 0;}/***************************************************User name: Result: AcceptedTake time: 60msTake Memory: 4484KBSubmit time: 2017-02-15 20:23:35****************************************************/

以下为time limit exceeded代码——深度优先搜索+有序邻接表

#include <stdio.h>#include <string.h>#include <stdlib.h>struct node{    int Data;    struct node *next;}*a[1004], *p, *q;int visit[1004], link[1004];int tp, op, flag;void DFS(int n){    if(n == 1)    {        flag = 1;        return;    }    if(a[n] == NULL)        return;    p = a[n]->next;    while(p != NULL)    {        if(visit[p->Data] == 0)        {            visit[p->Data] = 1;            DFS(p->Data);        }        p = p->next;    }}void BFS(int n){    if(n == 1)    {        flag = 1;        return;    }    op++;    p = a[n];    while(p != NULL)    {        if(visit[p->Data] == 0)        {            link[tp++] = p->Data;            visit[p->Data] = 1;        }        p = p->next;    }    if(op <= tp)        BFS(link[op]);}int main(){    int n, m, i, u, v;    while(scanf("%d %d", &n, &m) != EOF)    {        flag = 0;        tp = op = 0;        memset(visit, 0, sizeof(visit));        for(i = 0; i <= n; i++)            a[i] = NULL;        while(m--)        {            scanf("%d %d", &u, &v);            if(a[u] == NULL)            {                p = (struct node *)malloc(sizeof(struct node));                p->Data = u;                p->next = NULL;                a[u] = p;                p = (struct node *)malloc(sizeof(struct node));                p->Data = v;                p->next = NULL;                a[u]->next = p;            }            else            {                p = (struct node *)malloc(sizeof(struct node));                p->Data = v;                p->next = NULL;                q = a[u]->next;                if(q == NULL)                {                    p->next = a[u]->next;                    a[u]->next = p;                }                while(q != NULL)                {                    if(q->next == NULL)                    {                        if(p->Data >= q->Data)                        {                            p->next = q->next;                            q->next = p;                        }                    }                    else if(q->next != NULL)                    {                        if(p->Data >= q->Data && p->Data < q->next->Data)                        {                            p->next = q->next;                            q->next = p;                        }                    }                    q = q->next;                }            }        }        //DFS(n);        link[tp++] = n;        visit[n] = 1;        BFS(n);        if(flag)            printf("YES\n");        else            printf("NO\n");    }    return 0;}/***************************************************User name: Result: Time Limit ExceededTake time: 1010msTake Memory: 0KBSubmit time: 2017-02-15 20:12:04****************************************************/
0 0
原创粉丝点击