文章

1

粉丝

44

获赞

0

访问

238

头像
是否存在环 题解:
P1818 华南理工大学机试题
发布于2024年3月9日 18:53
阅读数 238

#include <stdio.h>
#include <stdbool.h>

#define MAX_NODES 1000

typedef struct {
    int dest;
    struct Node* next;
} Node;

typedef struct {
    Node* head;
} AdjList[MAX_NODES];

bool hasCycleUtil(int node, bool visited[], bool onPath[], AdjList graph, int n) {
    visited[node] = true;
    onPath[node] = true;

    Node* neighbor = graph[node].head;
    while (neighbor != NULL) {
        int nextNode = neighbor->dest;

        if (!visited[nextNode]) {
            if (hasCycleUtil(nextNode, visited, onPath, graph, n)) {
                return true;
            }
        } else if (onPath[nextNode]) {
            return true;
        }

      &...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发