-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst Contact.cpp
74 lines (72 loc) · 1.88 KB
/
First Contact.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
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
std::map<std::string, std::vector<std::string>> s;
bool edage[10000][10000];
struct node
{
std::string x, y;
} ans[350];
int count = 0;
bool cmp(node a, node b)
{
if (a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
int main()
{
int n, m;
std::cin >> n >> m;
for (int i = 0; i < m; i++)
{
std::string x, y;
std::cin >> x >> y;
edage[abs(std::stoi(x))][abs(std::stoi(y))] = true;
edage[abs(std::stoi(y))][abs(std::stoi(x))] = true;
if (x.size() == y.size())
{
s[x].push_back(y);
s[y].push_back(x);
}
}
int k;
std::cin >> k;
for (int i = 0; i < k; i++)
{
std::string x, y;
count = 0;
std::cin >> x >> y;
for (int j = 0; j < s[x].size(); j++)
{
if (s[x][j] != y)
{
for (int q = 0; q < s[y].size(); q++)
{
if (s[y][q] != x)
{
if (edage[abs(std::stoi(s[x][j]))][abs(std::stoi(s[y][q]))])
{
ans[count].x = s[x][j];
ans[count].y = s[y][q];
if (ans[count].x.size() == 5)
ans[count].x.erase(ans[count].x.begin());
if (ans[count].y.size() == 5)
ans[count].y.erase(ans[count].y.begin());
count++;
}
}
}
}
}
std::sort(ans, ans + count, cmp);
std::cout << count << std::endl;
for (int j = 0; j < count; j++)
{
printf("%s %s\n", ans[j].x.c_str(), ans[j].y.c_str());
}
}
return 0;
}