数据结构实验之图论三:判断可达性(SDUT 2138)(简单DFS)

2023-03-09 17:05:29 浏览数 (2)

代码语言:javascript复制
#include <bits/stdc  .h>
using namespace std;
int gra[1002][1005];
int vis[1002];
int n,m;
void dfs(int x)
{
    vis[x] = 1;
    for(int i = 1; i <= n ; i   )
    {
        if(gra[x][i] == 1 && !vis[i])
        {
            dfs(i);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(gra,0,sizeof(gra));
        memset(vis,0,sizeof(vis));
        int u,v;
        for(int i = 0; i < m; i   )
        {
            scanf("%d %d",&u,&v);
            gra[u][v] = 1;
        }
        dfs(n);
        if(vis[1] == 1)
            printf("YESn");
        else
            printf("NOn");
    }
    return 0;
}
dfs

0 人点赞