Complete the code to declare the adjacency list for the graph.
vector<vector<int>> graph[[1]];The adjacency list is declared with size n, which represents the number of nodes in the graph.
Complete the code to mark a node as visited during DFS.
visited[[1]] = true;i incorrectly.We mark the current node as visited to avoid revisiting it.
Fix the error in the DFS condition to detect a cycle.
if (visited[[1]] && neighbour != parent) return true;
The condition checks if the neighbour is visited and is not the parent, indicating a cycle.
Fill both blanks to complete the DFS recursive call and check for cycle.
if (!visited[[1]] && dfs([2], node, visited, graph)) return true;
The DFS is called recursively on the neighbour node, passing the current node as parent.
Fill all three blanks to complete the main function that detects cycle in the graph.
for (int [1] = 0; [2] < n; [3]++) { if (!visited[i] && dfs(i, -1, visited, graph)) { return true; } }
The loop variable i iterates over all nodes to start DFS if unvisited.
