题意:给出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. Eclipse和MyEclipse的区别

    翻译:日食,月食. eclipse是免费的,myeclipse是收费的. myeclipse是eclipse的插件.

  2. C++(十六) — 类中引用成员函数、命名空间的使用

    1.为什么类中引用成员函数? 类将属性和方法做了封装.类是一种数据类型,也就是:固定大小内存块的别名. 类的定义是一个抽象的概念,定义时不分配内存,当用类定义对象时,才分配一个固定大小的内存块. 此时 ...

  3. 三个大数据处理框架:Storm,Spark和Samza 介绍比较

    转自:http://www.open-open.com/lib/view/open1426065900123.html 许多分布式计算系统都可以实时或接近实时地处理大数据流.本文将对三种Apache框 ...

  4. opencv:鼠标操作

    示例程序: #include <opencv.hpp> using namespace cv; #define WINDOW_NAME "程序窗口" // ------ ...

  5. 八大排序算法的java实现

    有时间再贴算法分析图 JDK7的Collections.sort()的算法是TimSort, 适应性的归并排序, 比较晦涩难懂, 这里没有实现 public class mySort { // 冒泡排 ...

  6. Spring 自动装配;方法注入

    通过配置defalut—autowire属性,Spring IOC容器可以自动为程序注入Bean:默认是no(不启用自动装配). default—autowire的类型有: byName:通过名称自动 ...

  7. 记录下MD5加密遇到的坑

    错误的写法: public static String md5(String plainText) { byte[] secretBytes = null; try { secretBytes = M ...

  8. Agilent RF fundamentals (9)-Mixer basics

    Function: change the frequency of the incident RF key parameters ----------------------------------- ...

  9. Windows系统下MySQL解压版添加到系统服务

    MySQL软件版本:64位 5.7.12 1.首先配置MySQL的环境变量,在系统环境变量Path的开头添加MySQL的bin目录的路径,以“;”结束,我的路径配置如下: 2.修改MySQL根目录下的 ...

  10. AngularJs 中的transclude的理解

    Transclude是一个配置, 为了告诉AngularJs去获取当前指令模版内部的所有内容(实际使用ng-transclude), 更多关于怎么创建一个包含其他元素的指令: documentatio ...