-
Notifications
You must be signed in to change notification settings - Fork 0
/
1923.cpp
89 lines (65 loc) · 1.12 KB
/
1923.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//rerisson
#include <bits/stdc++.h>
using namespace std;
#define M 1002
map<string, int> m;
int id = 0;
int add(string s)
{
if(m.count(s)) return m[s];
return m[s] = id++;
}
vector<int> graph[M];
int n;
int dist[M];
void bfs(vector<int> graph[], int src, int size)
{
queue<int> next;
fill(dist, dist + n, INT_MAX);
next.push(src);
dist[src] = 0;
while(!next.empty())
{
int u = next.front();
next.pop();
for(int j = 0; j < (int)graph[u].size(); j++)
{
int v = graph[u][j];
if(dist[v] > dist[u] + 1)
{
dist[v] = dist[u] + 1;
next.push(v);
}
}
}
}
int main()
{
int ps, g;
string a, b;
cin >> ps >> g;
while(ps--)
{
cin >> a >> b;
graph[add(a)].push_back(add(b));
graph[add(b)].push_back(add(a));
}
n = m.size();
map<string, int>::iterator R;
R = m.find("Rerisson");
bfs(graph, R->second, n);
int p = 0;
for(int i = 0; i < n; i++)
{
if(dist[i] && dist[i] <= g)
p++;
}
printf("%d\n", p);
int i = 0;
for(auto it = m.begin(); it != m.end(); it++)
{
if(dist[it->second] && dist[it->second] <= g)
cout << it->first << endl;
i++;
}
}