题意是当年风气比较保守不像现在,如果一个男孩子A对一个女孩子B有好感,那么他需要跟他的好哥们C说,然后C再去找B的闺蜜,让闺蜜给B带话。
思路就是从A的同性朋友中找出C,再从B的同性朋友中找出D,然后C,D是好朋友的话,这个话就带到了。
但是输出那里规定A,B可以是同性,
If they are of the same gender, then both friends must be in the same gender as theirs.
emmmmmmm,这道题gay里gay气的。
坑点
1. 输入a,b的时候要处理一下,用字符串输入,不然可能会出现0000 和 -0000的情况
2. 需要避免C==B和D==A的情况
3. 输出的时候,要用%4d,因为题目规定的是4位数字
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
using namespace std;
map<int, map<int, bool> >Map;
struct node
{
int c,d;
}p[10015];
struct DOT
{
vector<int> same;
}dot[10015];
bool cmp(node a, node b)
{
if(a.c==b.c)
{
return a.d < b.d;
}
return a.c < b.c;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
long long a,b;
scanf("%lld%lld",&a,&b);
Map[abs(a)][abs(b)] = Map[abs(b)][abs(a)] = 1;
if((a<0&&b<0) || (a>0&&b>0))
{
dot[abs(a)].same.push_back(abs(b));
dot[abs(b)].same.push_back(abs(a));
}
}
int k;
scanf("%d",&k);
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
a = abs(a);
b = abs(b);
int cnt = 0;
for(int i=0;i<dot[a].same.size();i++)
{
for(int j=0;j<dot[b].same.size();j++)
{
if(a==dot[b].same[j] || b==dot[a].same[i]) continue;
if(Map[dot[a].same[i]][dot[b].same[j]])
{
p[cnt].c = dot[a].same[i];
p[cnt++].d = dot[b].same[j];
}
}
}
sort(p,p+cnt,cmp);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++)
{
printf("%04d %04d\n",p[i].c,p[i].d);
}
}
}