fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<list>
  4. #include<stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. struct graph{
  9. list<int> vertex;
  10. }*v;
  11.  
  12.  
  13. int main()
  14. {
  15.  
  16. int i,j,num_vertices,num_connected,m,counter,w,x,flag=0,c=0;
  17. list<int>::iterator it;
  18.  
  19. cout<<"Enter the number of vertices the graph contains..."<<endl;
  20. cin>>num_vertices;
  21.  
  22. v = new graph[num_vertices];
  23.  
  24. if (v == 0)
  25. cout << "Error: memory could not be allocated";
  26.  
  27. for(i=1;i<=num_vertices;i++)
  28. {
  29. cout<<"Enter the number of vertices to which vertex "<< i <<" is connected"<<endl;
  30. cin>>num_connected;
  31.  
  32. for(j=1;j<=num_connected;j++)
  33. {
  34. cin>>m;
  35. (v+i)->vertex.push_back(m);
  36. }
  37. }
  38.  
  39. for(i=1;i<=num_vertices;i++)
  40. {
  41. for(it= (v+i)->vertex.begin();it!= (v+i)->vertex.end();it++)
  42. cout<<*it<<"->";
  43. cout<<endl;
  44. }
  45.  
  46. return 0;
  47.  
  48. }
  49.  
Runtime error #stdin #stdout 0.01s 2816KB
stdin
3
2
2 3
2
3 1
2
1 2
stdout
Enter the number of vertices the graph contains...
Enter the number of vertices to which  vertex 1 is connected
Enter the number of vertices to which  vertex 2 is connected
Enter the number of vertices to which  vertex 3 is connected