Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Grafo
- {
- vector <vector <int> > adj; ///Esta es nuestra lista de adyacencia
- void leer()
- {
- int n, m;
- cin >> n >> m; ///Leo nodos y aristas
- adj.resize(n+1); ///Le doy tamaño a la lista
- int desde, hasta;
- for(int i=0; i<m; i++)
- {
- cin >> desde >> hasta;
- adj[desde].push_back(hasta);
- adj[hasta].push_back(desde); ///Suponiendo que es bidireccional
- }
- }
- void mostrar()
- {
- cout << "Lista de adyacencia: " << endl;
- for(int i=1; i<adj.size(); i++)
- {
- cout << "Adj[" << i << "]: ";
- for(int j=0; j<adj[i].size(); j++)
- {
- cout << adj[i][j] << " ";
- }
- cout << endl;
- }
- }
- };
- int main()
- {
- Grafo g;
- g.leer();
- g.mostrar();
- /**
- Input de ejemplo:
- 5 4
- 1 2
- 1 3
- 1 4
- 1 4
- Output de ejemplo:
- Lista de adyacencia:
- Adj[1]: 2 3 4 4
- Adj[2]: 1
- Adj[3]: 1
- Adj[4]: 1 1
- Adj[5]:
- **/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment