Advertisement
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;
- vector <bool> v; ///Vector de visitados
- int nodos, aristas;
- void leer()
- {
- cin >> nodos >> aristas;
- adj.resize(nodos+1);
- v = vector <bool> (nodos+1, false);
- int desde, hacia;
- for(int i=0; i<aristas; i++)
- {
- cin >> desde >> hacia;
- adj[desde].push_back(hacia);
- adj[hacia].push_back(desde);
- }
- }
- void BFS(int n)
- {
- v[n] = true; ///Marco como visitado
- for(int i=0; i<adj[n].size(); i++)
- if(v[adj[n][i]] == false)
- DFS(adj[n][i]);
- }
- int resolver()
- {
- DFS(1);
- int cant = 0;
- for(int i=1; i<v.size(); i++)
- if(v[i] == true)
- cant++;
- return cant;
- }
- };
- int main()
- {
- /**
- Problema:
- Dado un grafo, decir cuales son los nodos que son alcanzables desde el nodo 1.
- **/
- Grafo g;
- g.leer();
- cout << g.resolver() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement