Complete the code to declare the adjacency list for the graph.
vector<vector<int>> graph([1]);The adjacency list is a vector of vectors sized by the number of nodes n.
Complete the code to mark a node as visited inside BFS.
visited[[1]] = true;We mark the current node as visited to avoid revisiting it.
Fix the error in the BFS loop condition to process nodes until the queue is empty.
while (![1].empty()) {
The BFS loop continues while the queue has nodes to process.
Fill both blanks to add neighbors to the queue if not visited.
for (int [1] : graph[node]) { if (!visited[[2]]) { queue.push([2]); visited[[2]] = true; } }
We iterate over each neighbor of the current node and add it to the queue if not visited.
Fill all three blanks to count connected components using BFS.
int count = 0; for (int i = 0; i < [1]; i++) { if (!visited[i]) { queue.push(i); visited[i] = true; while (!queue.[2]()) { int node = queue.front(); queue.[3](); for (int neighbor : graph[node]) { if (!visited[neighbor]) { queue.push(neighbor); visited[neighbor] = true; } } } count++; } }
We loop over all n nodes, use queue.empty() to check if BFS continues, and queue.pop() to remove the front node.
