1139 First Contact(30 分)
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly in the first place. Instead, he might ask another boy C, one of his close friends, to ask another girl D, who was a friend of both B and C, to send a message to B – quite a long shot, isn’t it? Girls would do analogously.
题意比较好懂 同性交友了解一下??
坑的地方就是-0000这个例子 要用string搞
#include <bits/stdc++.h>
using namespace std;
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define mp(a,b) make_pair(a,b)
#define db(a) (cout<<"***"<<a<<endl)
#define pb push_back
#define maxn 99200
typedef pair<int,int> Pair;
int boy[maxn];
vector<int>v[maxn];
set<int>s[maxn];
bool diff(int a,int b){
return boy[a]!=boy[b];
}
set<Pair> st;
int main(){
int n,m;
cin>>n>>m;
FORP(i,0,maxn) boy[i]=1;//all boys
FORP(i,1,m){
string aa,bb;
cin>>aa>>bb;
int a,b;
sscanf(aa.c_str(),"%d",&a);
sscanf(bb.c_str(),"%d",&b);
if(aa.size()==5) boy[abs(a)]=-1;
if(bb.size()==5) boy[abs(b)]=-1;
a=abs(a); b=abs(b);
v[a].pb(b);
s[a].insert(b);
v[b].pb(a);
s[b].insert(a);
}
int k;cin>>k;
FORP(i,1,k){
int a,b;
cin>>a>>b;
a=abs(a);
b=abs(b);
vector<int> fir;//第一个请求对象
vector<int> sec;//第er个请求对象
vector<Pair> ans;
st.clear();
for(int j=0;j<v[a].size();j++){
int to=v[a][j];
if(!diff(a,to)) fir.pb(to);
}
for(int j=0;j<v[b].size();j++){
int to=v[b][j];
if(!diff(b,to)) sec.pb(to);
}
for(int j=0;j<fir.size();j++){
int to=fir[j];
for(int q=0;q<sec.size();q++){
int final=sec[q];
if(s[to].find(final)!=s[to].end()&&to!=b&&final!=a){
st.insert(mp(to,final));
}
}
}
cout<<st.size()<<endl;
set<Pair>::iterator It;
for(It=st.begin();It!=st.end();++It){
printf("%04d %04d\n",(*It).first,(*It).second );
}
}
return 0;
}