代码:
//本题目很重要的一点是读懂题目
//1. A 要找他的同性朋友C,并且这个朋友不是间接朋友,一定是两者之间有一条边
//然后再找与B 同性的朋友,该朋友一定是C的朋友,但是不一定是他的朋友
//2. 朋友C不是B
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
#include<cmath>
#include<unordered_set>
using namespace std;
unordered_map<string,unordered_set<string> >res;--------》这种形式输入,unordered_set<>
int n,m;
string a,b;
typedef pair<int,int>pii;
int main(){
cin>>n>>m;
while(m--){
cin>>a>>b;--------------》必须以字符串类型输入,因为会有-0000的情况
res[a].insert(b);
res[b].insert(a);
}
int k;
cin>>k;
while(k--){
cin>>a>>b;
vector<pii>ans;
for(auto &t:res[a]){
if(t.size()!=a.size()||t==b){
continue;
}
for(auto &r:res[b]){
if(r.size()!=b.size()||r==b||r==a||r==t){
continue;
}
if(res[t].count(r))
ans.push_back({abs(atoi(t.c_str())),abs(atoi(r.c_str()))});
}
}
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(auto &r:ans){
printf("%04d %04d\n",r.first,r.second);
}
}
return 0;
}