题意:给出n个人,m对朋友关系,其中带负号的表示女孩。然后给出k对查询a,b,要求找出所有a的同性别好友c,以及b的同性别好友d,且c和d又是好友关系。输出所有满足条件的c和d,按c的升序输出,若c编号相同则按d的升序输出。

思路:其实不难,30分的题有点被唬住了。

对于每一个查询a,b,先分别找出a同性好友friend[a],b的同性好友friend[b],然后在friend[a]和friend[b]中找出具有朋友关系的c,d即可。注意,根据题意,-0000和0000是不一样的,若以int类型数据读入,则均表示为0,从而无法区别性别,因此读入数据应该以字符串的类型读入。两者性别是否一致可通过其字符串的长度是否一致来区别。最后,若a,b为同性时,存在a的好友就是b,或b的好友就是a的特殊情况,应该排除掉。因为a要向b联系必须通过两个中间人!

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <map>
#include <set>
#include <vector>
using namespace std;

unordered_map<int,unordered_map<int,bool>> mp;//表示朋友关系的映射
map<int,set<int>> friendOfBoy,friendOfGirl;

int main()
{
    //freopen("pat.txt","r",stdin);
    int n,m,k;
    scanf("%d%d",&n,&m);
    ],b[];
    ;i<m;i++){
        scanf("%s%s",a,b);
        int u=abs(atoi(a)), v=abs(atoi(b));
        mp[v][u]=mp[u][v]=true;
        if(strlen(a)==strlen(b)){
            ]=='-'){
                friendOfGirl[u].insert(v);
                friendOfGirl[v].insert(u);
            }else{
                friendOfBoy[u].insert(v);
                friendOfBoy[v].insert(u);
            }
        }
    }
    scanf("%d",&k);
    ;i<k;i++){
        set<int> friendA,friendB;
        vector<pair<int,int>> ans;
        scanf("%s%s",a,b);
        int u=abs(atoi(a)), v=abs(atoi(b));
        if(strlen(a)==strlen(b)){
            ]=='-'){
                friendA=friendOfGirl[u];
                friendB=friendOfGirl[v];
            }else{
                friendA=friendOfBoy[u];
                friendB=friendOfBoy[v];
            }
        }]=='-'){
            friendA=friendOfGirl[u];
            friendB=friendOfBoy[v];
        }else{
            friendA=friendOfBoy[u];
            friendB=friendOfGirl[v];
        }
        for(auto p:friendA){
            for(auto q:friendB){
                if(p==v || q==u) continue;
                if(mp[p][q]) ans.push_back(pair<int,int>(p,q));
            }
        }
        printf("%d\n",ans.size());
        for(auto it:ans)
            printf("%04d %04d\n",it.first,it.second);
    }
    ;
}

1139 First Contact的更多相关文章

  1. PAT 1139 First Contact[难][模拟]

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  2. 1139 First Contact(30 分)

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  3. PAT 1139 First Contact

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  4. PAT甲级1139 First Contact

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...

  5. pat advanced 1139. First Contact (30)

    题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...

  6. 1139 First Contact PAT (Advanced Level)

    原题链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 测试点分析: 首先来分析一下测试 ...

  7. pat甲级1139

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  8. PTA 1139 1138 1137 1136

    PAT 1139 1138 1137 1136 一个月不写题,有点生疏..脑子跟不上手速,还可以啦,反正今天很开心. PAT 1139 First Contact 18/30 找个时间再修bug 23 ...

  9. PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟

    1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...

随机推荐

  1. RxJava+RxAndroid+MVP入坑实践(基础篇)

    转载请注明出处:http://www.blog.csdn.net/zhyxuexijava/article/details/51597230.com 前段时间看了MVP架构和RxJava,最近也在重构 ...

  2. SQL正则表达式

    (转自:http://blog.csdn.net/xu1314/article/details/10174067) 当我们要进行一些简单的糊涂查询时用百分号(%),通配符(_)就可以了.其中%表达任意 ...

  3. 前端之CSS续集

    CSS:语法形式上由选择器+以及一条或多条声明组成:选择器查找到指定的html标签后,使用css属性设置html标签的样式: 一.css 语法形式: 二.使用步骤   1.引入css规则 2.使用cs ...

  4. SWF文件加密、混淆

    简单说下SWF文件的混淆原理,(已经明白的请跳过本段):我们的AS源代码被编译完成后,SWF内部会形成一个字符串映射表,包含源码里出现的所有字符串(类名,包名,成员变量名,常量等).一个数字(相当于地 ...

  5. http keep - alive 与 长连接

    http1.0 2.0 1.1区别 你可以把 WebSocket 看成是 HTTP 协议为了支持长连接所打的一个大补丁,它和 HTTP 有一些共性,是为了解决 HTTP 本身无法解决的某些问题而做出的 ...

  6. React Native组件(一)组件的生命周期

    相关文章 React Native探索系列 前言 React Native有很多组件比如Image.ListView等等,想要合理的使用组件,首先要先了解组件的生命周期. 1.概述 无论你是开发And ...

  7. 在Wifi网络中嗅探明文密码(HTTP POST请求、POP等)

    全世界,现在大约50%的网站没有使用SSL加密,天朝尤其多. 我们都知道通过HTTP发送的数据都是明文,没有使用任何加密,即使是在数据敏感的登录页面. 本文的目的是:如果你在不熟悉的网络环境中,要注意 ...

  8. LINUX下的ssh登录之后的文件远程copy:scp命令(接前文ssh登录)

    先记录参考: 1:http://www.cnblogs.com/peida/archive/2013/03/15/2960802.html 2:http://www.vpser.net/manage/ ...

  9. Spring字符编码过滤器

    1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class ...

  10. STL源码解析之vector自实现

    最近仔细阅读了下侯捷老师的<STL源码剖析>的前三章内容:空间配置器.迭代器.traits技术,为了加强理解和掌握,特参照源码自实现Vector,以下对相关重点知识进行说明. 1. vec ...