Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///https://trello.com/c/0Qqn6lbD/21-timus-1837-isenbaevs-number
- ///http://acm.timus.ru/problem.aspx?space=1&num=1837
- #include <iostream>
- #include <vector>
- #include <map>
- #include <queue>
- #define INF (1<<29)
- using namespace std;
- struct graf {
- vector< vector<long> > ady;
- map<string,long> mapa;
- vector<long> dist;
- long n, m;
- long dar_id (string pal)
- {
- if (mapa[pal]==0)
- mapa[pal]=mapa.size();
- return mapa[pal];
- }
- void BFS (long inicial)
- {
- queue<long> cola;
- cola.push(inicial);
- dist[inicial]=0;
- while( cola.size() )
- {
- long nodo = cola.front();
- cola.pop();
- for(long i=0; i<ady[nodo].size(); i++)
- {
- long vecino = ady[nodo][i];
- if ( dist[nodo]+1 < dist[vecino] )
- {
- dist[vecino] = dist[nodo]+1;
- cola.push(vecino);
- }
- }
- }
- for(auto i:mapa)
- {
- cout<<i.first<<" ";
- if(dist[i.second]!=INF)
- cout<<dist[i.second]<<endl;
- else cout<<"undefined"<<endl;
- }
- }
- void leer ()
- {
- cin>>m;
- ady.resize(301);
- string A, B, C;
- for(long i=0; i<m; i++)
- {
- cin>>A>>B>>C;
- long a = dar_id(A);
- long b = dar_id(B);
- long c = dar_id(C);
- ady[a].push_back(b);
- ady[a].push_back(c);
- ady[b].push_back(a);
- ady[b].push_back(c);
- ady[c].push_back(a);
- ady[c].push_back(b);
- }
- n = mapa.size();
- ady.resize(n+1);
- dist = vector<long> (n+1,INF);
- BFS( mapa["Isenbaev"] );
- }
- };
- int main ()
- {
- graf g;
- g.leer();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment