forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10044.cpp
115 lines (107 loc) · 1.67 KB
/
10044.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
using namespace std;
#define MAX 5000
vector<int> G[MAX];
int n, m;
bool V[MAX];
map<string, int> A;
struct Step
{
int x, v;
Step() {}
Step(int x, int v) : x(x), v(v) {}
};
queue<Step> Q;
int author(const string &a)
{
if (A.find(a) != A.end())
{
return A[a];
}
else
{
return A[a] = A.size() - 1;
}
}
char C[MAX];
void parseAuthors(const string &s)
{
vector<int> TA;
int commas = 0, chars = 0;
for (int i = 0; i < s.size(); i++)
{
char c = s[i];
if (chars == 0 && c == ' ')
{
continue;
}
if ((c == ',' || c == ':') && ++commas == 2)
{
TA.push_back(author(string(C, chars)));
chars = commas = 0;
}
else
{
C[chars++] = c;
}
}
for (int i = 0; i < TA.size(); i++)
{
for (int j = i + 1; j < TA.size(); j++)
{
G[TA[i]].push_back(TA[j]);
G[TA[j]].push_back(TA[i]);
}
}
}
int main()
{
string s;
int t = 0, tt;
cin >> tt;
while (t++ < tt)
{
cin >> n >> m;
memset(G, 0, sizeof(G));
A.clear();
getline(cin, s);
while (n--)
{
getline(cin, s);
parseAuthors(s);
}
cout << "Scenario " << t << endl;
for (int i = 0; i < m; i++)
{
bool stop;
memset(V, 0, sizeof(V));
getline(cin, s);
int b = author(s);
Q = queue<Step>();
Q.push(Step(author("Erdos, P."), 0));
bool found = false;
while (!Q.empty())
{
Step it = Q.front();
Q.pop();
if (it.x == b)
{
cout << s << " " << it.v << endl;
found = true;
break;
}
V[it.x] = true;
for (int i = 0; i < G[it.x].size(); i++)
if (!V[G[it.x][i]])
{
Q.push(Step(G[it.x][i], it.v + 1));
}
}
if (!found)
{
cout << s << " infinity" << endl;
}
}
}
return 0;
}