1139 First Contact (30 分)
思路有空更新
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
struct node{
int x, y;
};
vector<int> v[10000];
map<int, map<int, int>> m;
bool cmp(node a, node b){
if(a.x != b.x)
return a.x < b.x;
else
return a.y < b.y;
}
int main() {
int n, M, k;
cin >> n >> M;
while(M--) {
string a, b;
cin >> a >> b;
int x = abs(stoi(a)), y = abs(stoi(b));
if(a.length() == b.length()){
v[x].push_back(y);
v[y].push_back(x);
}
m[x][y] = m[y][x] = 1;
}
cin >> k;
while(k--) {
string a, b;
cin >> a >> b;
int x = abs(stoi(a)), y = abs(stoi(b));
vector<node> ans;
for(int i = 0; i < v[x].size(); ++i)
for(int j = 0; j < v[y].size(); ++j)
if(m[v[x][i]][v[y][j]] == 1 && v[x][i] != y && v[y][j] != x)
ans.push_back({v[x][i], v[y][j]});
sort(ans.begin(), ans.end(), cmp);
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); ++i)
printf("%04d %04d\n", ans[i].x, ans[i].y);
}
}