Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- struct graf {
- vector< vector<int> > adj;
- vector<bool> visit;
- string linea;
- int k, n;
- void DFS (int nodo)
- {
- visit[nodo] = true;
- for (int i=0; i<adj[nodo].size(); i++)
- {
- int vecin = adj[nodo][i];
- if ( !visit[vecin] )
- DFS(vecin);
- }
- }
- int contar_componentes()
- {
- int c = 0;
- for (int i=0; i<n; i++)
- if (!visit[i])
- {
- c++;
- DFS(i);
- }
- return c;
- }
- void read ()
- {
- cin>>k;
- getline(cin,linea);
- getline(cin,linea);
- for (int i=0; i<k; i++)
- {
- getline(cin,linea);
- n = 1 + linea[0] - 'A';
- adj.clear();
- adj.resize(n);
- visit = vector<bool> (n, false);
- getline(cin,linea);
- while ( linea.size() )
- {
- int a = linea[0]-'A';
- int b = linea[1]-'A';
- adj[a].push_back(b);
- adj[b].push_back(a);
- getline(cin,linea);
- }
- cout << contar_componentes();
- if ( (1+i)<(k) )
- cout << endl << endl;
- }
- }
- };
- int main()
- {
- graf g;
- g.read();
- return 0;
- } /*
- 2
- E
- AB
- CE
- DB
- EC
- G
- AB
- BC
- CD
- EF
- */
Advertisement
Add Comment
Please, Sign In to add comment