1139 First Contact(30 分)

题意是当年风气比较保守不像现在,如果一个男孩子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);
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值