Advertisement
Rakibul_Ahasan

Adjacency list

Nov 11th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3.  
  4.     using namespace std;
  5.  
  6.     vector <int> adj[10];
  7.  
  8.     int main()
  9.     {
  10.         int x, y, nodes, edges;
  11.         cin >> nodes;       //Number of nodes
  12.         cin >> edges;       //Number of edges
  13.         for(int i = 0;i < edges;++i)
  14.         {
  15.                 cin >> x >> y;
  16.             adj[x].push_back(y);        //Insert y in adjacency list of x
  17.          }
  18.       for(int i = 1;i <= nodes;++i)
  19.         {
  20.             cout << "Adjacency list of node " << i << ": ";
  21.         for(int j = 0;j < adj[i].size();++j)
  22.             {
  23.             if(j == adj[i].size() - 1)
  24.                     cout << adj[i][j] << endl;
  25.             else
  26.                 cout << adj[i][j] << " --> ";
  27.          }
  28.        }
  29.     return 0;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement