Advertisement
informaticage

Adj list implementation c++

Feb 10th, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void inserisciArco ( vector<vector<int>> &LA, int sorgente, int destinazione ) {
  7.     LA [ sorgente ].push_back ( destinazione );
  8. }
  9.  
  10. void stampaGrafo ( vector<vector<int>> LA ) {
  11.     for ( int i = 0; i < LA.size(); i++ ) {
  12.         cout << i << " -> ";
  13.         for ( int j = 0; j < LA[ i ].size(); j++ ) {
  14.             cout << LA[i][j] << " ";
  15.         }
  16.         cout << endl;
  17.     }
  18. }
  19.  
  20. int main( void ) {
  21.     int numeroVertici, numeroArchi;
  22.     cin >> numeroVertici >> numeroArchi;
  23.  
  24.     vector<vector<int>> LA ( numeroVertici );
  25.  
  26.     for ( int i = 0; i < numeroArchi; i ++ ) {
  27.         int partenza, destinazione;
  28.         cin >> partenza >> destinazione;
  29.         inserisciArco ( LA, partenza, destinazione );
  30.     }
  31.  
  32.     stampaGrafo( LA );
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement