fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function to add an edge to the graph
  5. void addEdge(vector<vector<int>> &adjList, int u, int v) {
  6. adjList[u].push_back(v); // Add v to u's list
  7.  
  8. }
  9.  
  10. // Function to print the adjacency list representation of the graph
  11. void printGraph(const vector<vector<int>> &adjList) {
  12. for (int i = 0; i < adjList.size(); i++) {
  13. cout << "Vertex " << i << ":";
  14. for (int j : adjList[i]) {
  15. cout << " -> " << j;
  16. }
  17. cout << endl;
  18. }
  19. }
  20.  
  21. int main() {
  22. int numVertices = 6; // Number of vertices in the graph
  23.  
  24. // Create an adjacency list
  25. vector<vector<int>> adjList(numVertices);
  26.  
  27. // Add edges
  28. addEdge(adjList, 2, 0);
  29. addEdge(adjList, 2, 1);
  30. addEdge(adjList, 2, 4);
  31. addEdge(adjList, 2, 5);
  32. addEdge(adjList, 3, 0);
  33. addEdge(adjList, 3, 1);
  34. addEdge(adjList, 3, 4);
  35. addEdge(adjList, 3, 5);
  36. addEdge(adjList, 4, 0);
  37. addEdge(adjList, 4, 1);
  38. addEdge(adjList, 5, 0);
  39. addEdge(adjList, 5, 1);
  40.  
  41.  
  42. // Print the adjacency list
  43. printGraph(adjList);
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Vertex 0:
Vertex 1:
Vertex 2: -> 0 -> 1 -> 4 -> 5
Vertex 3: -> 0 -> 1 -> 4 -> 5
Vertex 4: -> 0 -> 1
Vertex 5: -> 0 -> 1