Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //* Alen Antonelli 2018*/ https://www.hackerrank.com/challenges/components-in-graph/problem
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- struct grafo {
- vector< vector<int> > adj;
- map<int, int> mapa;
- int N;
- int dar_id (int x)
- {
- if(mapa[x]==0)
- mapa[x]=mapa.size();
- return mapa[x];
- }
- void leer()
- {
- cin>>N;
- adj.resize(N+1);
- int a, b;
- int desde, hasta;
- for (int i=0; i<N; i++)
- {
- cin>>a>>b;
- desde = dar_id(a);
- hasta = dar_id(b);
- adj[desde].push_back(hasta);
- adj[hasta].push_back(desde);
- }
- }
- void mostrar()
- {
- /*for(int i=1; i<=N; i++)
- {
- cout<< endl << i << "; ";
- for(int j=1; j<=adj[i].size(); j++)
- cout<<adj[i][j]<<", ";
- } */
- }
- };
- int main()
- {
- grafo g;
- g.leer();
- g.mostrar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment