-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy path1139. First Contact (30).cpp
45 lines (45 loc) · 1.33 KB
/
1139. First Contact (30).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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
unordered_map<int, bool> arr;
struct node {
int a, b;
};
bool cmp(node x, node y) {
return x.a != y.a ? x.a < y.a : x.b < y.b;
}
int main() {
int n, m, k;
scanf("%d%d", &n, &m);
vector<int> v[10000];
for (int i = 0; i < m; i++) {
string a, b;
cin >> a >> b;
if (a.length() == b.length()) {
v[abs(stoi(a))].push_back(abs(stoi(b)));
v[abs(stoi(b))].push_back(abs(stoi(a)));
}
arr[abs(stoi(a)) * 10000 + abs(stoi(b))] = arr[abs(stoi(b)) * 10000 + abs(stoi(a))] = true;
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
vector<node> ans;
for (int j = 0; j < v[abs(c)].size(); j++) {
for (int k = 0; k < v[abs(d)].size(); k++) {
if (v[abs(c)][j] == abs(d) || abs(c) == v[abs(d)][k]) continue;
if (arr[v[abs(c)][j] * 10000 + v[abs(d)][k]] == true)
ans.push_back(node{v[abs(c)][j], v[abs(d)][k]});
}
}
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", int(ans.size()));
for(int j = 0; j < ans.size(); j++)
printf("%04d %04d\n", ans[j].a, ans[j].b);
}
return 0;
}