-
Notifications
You must be signed in to change notification settings - Fork 0
/
1837. Isenbaev's Number(bfs).cpp.bak
73 lines (71 loc) · 1.56 KB
/
1837. Isenbaev's Number(bfs).cpp.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cassert>
#include <cstring>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
set<string> values;
set<string> data[100];
map<string,int> answer;
int k=0;
while(t--)
{
string a,b,c;
cin>>a>>b>>c;
values.insert(a);
values.insert(b);
values.insert(c);
data[k].insert(a);
data[k].insert(b);
data[k].insert(c);
k++;
}
queue<string> pn;
queue<int> dist;
pn.push("Isenbaev");
dist.push(0);
while(!pn.empty())
{
string prname=pn.front();
pn.pop();
int distance=dist.front();
dist.pop();
if(answer.find(prname)!=answer.end())
continue;
// cout<<prname<<" "<<distance<<endl;
for(int i=0;i<k;i++){
if(data[i].find(prname)!=data[i].end()){
for(set<string>::iterator j=data[i].begin();j!=data[i].end();j++){
pn.push(*j);
dist.push(distance+1);
}
}
}
answer[prname]=distance;
}
//cout<<"YO";
for(set<string>::iterator it=values.begin();it!=values.end();it++){
if(answer.find(*it)==answer.end()){
cout<<*it<<" "<<"undefined"<<endl;
}
else{
cout<<(*it)<<" "<<answer[*it]<<endl;
}
}
return 0;
}