【PAT甲级】1139 First Contact

简介: 【PAT甲级】1139 First Contact

1139 First Contact

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.


Here given a network of friendship relations, you are supposed to help a boy or a girl to list all their friends who can possibly help them making the first contact.


Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (1 < N ≤ 300) and M, being the total number of people and the number of friendship relations, respectively. Then M lines follow, each gives a pair of friends. Here a person is represented by a 4-digit ID. To tell their genders, we use a negative sign to represent girls.


After the relations, a positive integer K (≤ 100) is given, which is the number of queries. Then K lines of queries follow, each gives a pair of lovers, separated by a space. It is assumed that the first one is having a crush on the second one.


Output Specification:

For each query, first print in a line the number of different pairs of friends they can find to help them, then in each line print the IDs of a pair of friends.


If the lovers A and B are of opposite genders, you must first print the friend of A who is of the same gender of A, then the friend of B, who is of the same gender of B. If they are of the same gender, then both friends must be in the same gender as theirs. It is guaranteed that each person has only one gender.


The friends must be printed in non-decreasing order of the first IDs, and for the same first ones, in increasing order of the seconds ones.


Sample Input:

10 18
-2001 1001
-2002 -2001
1004 1001
-2004 -2001
-2003 1005
1005 -2001
1001 -2003
1002 1001
1002 -2004
-2004 1001
1003 -2002
-2003 1003
1004 -2002
-2001 -2003
1001 1003
1003 -2001
1002 -2001
-2002 -2003
5
1001 -2001
-2003 1001
1005 -2001
-2002 -2004
1111 -2003


Sample Output:

4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0

题意

如果 A 暗恋 B ,则 A 先会联系其同性朋友 C ,然后让 C 去联系 B 的同性朋友 D(D 也是 C 的朋友),让 D 拜托 B 和 A 出来约会,而 C 和 D 就称为第一次联系的朋友,关系网如下:


A -> C -> D -> B


在给定的友谊关系网络中,请你帮助一些男孩和女孩列出所有可能会帮助他们进行第一次联系的朋友。


每个人都用一个 4 位数字表示,为了区分性别,我们在女生的编号前加了负号。


如果 A 和 B 的性别不同,则先输出与 A 性别相同的朋友,再输出与 B 性别相同的朋友。


如果 A 和 B 的性别相同,则两个朋友的性别必须也和他们相同。


保证每个人只有一种性别。


一组询问内,朋友对按第一个朋友的编号从小到大进行排序,第一个朋友的编号相同时,按第二个朋友的编号从小到大进行排序。


思路

1.这道题给定的人数范围是 300 以内,所以我们可以用邻接矩阵来存储给定的图。但是每个人用编号来表示,故需要先将每个人映射在 1~300 的下标之间。用映射后的下标再去关系记录到邻接矩阵当中,并且用两个数组来记录每个人是男孩还是女孩。


需要注意的是,女孩的编号为负号,需要先处理再去映射,同时也要另外用一个数组来记录下标在映射之前对应的编号是什么,方便后续查找。


2.当输入完信息后,需要对记录男孩和女孩的数组进性排序和去重,因为在输入时,每个编号都加到数组中,其中可能会存在重复的值。


3.查询给定两人的第一次联系的朋友,需要先去判断两人的性别再进行遍历。遍历时如果找到满足 A -> C -> D -> B 的答案,则将答案加入到答案数组中。


4.最后对答案进行排序,并输出最终的结果。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 310;
int n, m, id;
unordered_map<string, int> mp;
string num[N];
bool g[N][N];
vector<int> boys, girls;
int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        string a, b;
        cin >> a >> b;
        string x = a, y = b;
        //如果有负号,则去掉负号
        if (x.size() == 5) x = x.substr(1);
        if (y.size() == 5) y = y.substr(1);
        //对编号进行映射
        if (mp.count(x) == 0)  mp[x] = ++id, num[id] = x;
        if (mp.count(y) == 0)  mp[y] = ++id, num[id] = y;
        //记录映射后的边
        int px = mp[x], py = mp[y];
        g[px][py] = g[py][px] = true;
        //记录两人是男孩还是女孩
        if (a[0] != '-')   boys.push_back(px);
        else    girls.push_back(px);
        if (b[0] != '-')   boys.push_back(py);
        else    girls.push_back(py);
    }
    //删除重复加入的编号
    sort(boys.begin(), boys.end());
    boys.erase(unique(boys.begin(), boys.end()), boys.end());
    sort(girls.begin(), girls.end());
    girls.erase(unique(girls.begin(), girls.end()), girls.end());
    //开始查询
    int k;
    cin >> k;
    while (k--)
    {
        vector<pair<string, string>> res;
        string x, y;
        cin >> x >> y;
        //判断查询的两人的性别
        vector<int> p = boys, q = boys;
        if (x[0] == '-')   p = girls, x = x.substr(1);
        if (y[0] == '-')   q = girls, y = y.substr(1);
        int a = mp[x], b = mp[y];    //获取映射后的编号
        //遍历所有人,找到满足要求的答案
        for (auto& c : p)
            for (auto& d : q)
                if (a != c && a != d && b != c && b != d && g[a][c] && g[c][d] && g[d][b])
                    res.push_back({ num[c],num[d] });
        sort(res.begin(), res.end());    //对结果进行排序
        cout << res.size() << endl; //先输出答案数量
        for (auto& t : res)    //输出所有答案
            cout << t.first << " " << t.second << endl;
    }
    return 0;
}


目录
相关文章
|
存储 安全 网络协议
【内网安全-横向移动】IPC$连接--->计划任务--->上线
【内网安全-横向移动】IPC$连接--->计划任务--->上线
1227 0
【内网安全-横向移动】IPC$连接--->计划任务--->上线
|
人工智能 自然语言处理 搜索推荐
6个好用的AI写作工具合集,各种AI写作软件类型超全整理!
AI生成营销文案、生成文章标题、改写润色内容、写作素材搜集...好用的AI写作工具有哪些?
6个好用的AI写作工具合集,各种AI写作软件类型超全整理!
|
3月前
|
人工智能 自然语言处理 安全
去不掉的 AI 味儿
本文剖析AI写作的“统一味儿”成因:非语言技巧问题,而是预训练与后训练中概率分布被反复抹平所致。指出“去AI味”本质是伪命题,真正出路在于人机协作——人做树干(真实经验、情绪、毛边),AI做枝叶(整理、润色)。适合会议纪要等需清晰性的场景,但文学、评论等高熵领域,唯有真实生命痕迹不可替代。(239字)
564 0
|
3月前
|
缓存 API 开发工具
通义千问Qwen大模型深度解析:Qwen3.8‑Max/Flash/Omni能力矩阵,RAG微调、智能体开发、代码调用实战、与企业落地完整指南
生成式AI已经从简单问答对话,进化为可以处理超长文档、图文音视频混合输入、自主拆解目标完成多步骤业务的通用智能底座。通义千问Qwen3.8完整家族形成了分层能力矩阵,覆盖旗舰推理、均衡通用、高速高吞吐、原生全模态视听、专项编程多类基座,既可以普通用户网页端直接交互体验,也可以通过百炼平台API集成进业务系统,同时开放开源权重,支持本地私有化部署,覆盖个人创作者、独立开发者、中小企业、大型政企的差异化诉求。
1117 0
|
9月前
|
消息中间件 弹性计算 监控
在阿里云上搭建低延迟行情监控系统(WebSocket实战)
本文详解如何在阿里云ECS(Ubuntu 22.04)上用Python构建生产级WebSocket行情客户端:支持自动重连、心跳保活、多市场(股票/加密货币)实时订阅,并通过消息队列解耦处理,显著提升稳定性与低延迟。
|
10月前
|
SQL 关系型数据库 数据挖掘
PostgreSQL窗口函数从入门到实操
PostgreSQL窗口函数是数据分析利器:统计不丢行、细节全保留!本文用大白话讲透核心概念(分区/排序/窗口帧),配可运行示例,带你零基础掌握排名、移动平均、累积求和等高频场景,学完即用。
|
12月前
|
小程序 Java 关系型数据库
基于微信小程序的家政服务系统
本项目基于微信小程序,结合Spring Boot与Java技术,构建一个集家政服务预约、订单管理、用户评价及社区互助于一体的数字化平台,旨在提升家政服务效率与用户体验,推动行业智能化转型。
|
12月前
|
存储 运维 监控
存储分层降低成本实践
《存储分层降低成本实践指南》系统阐述企业如何通过数据分层管理优化存储成本。结合数据访问频率、业务需求与成本模型,指导企业构建标准、低频、归档及冷归档多级存储体系,借助生命周期规则、智能迁移与监控工具,实现热、温、冷数据自动流转,在保障性能的同时显著降低总体存储支出,助力数字化转型降本增效。(238字)
763 0
|
云安全 人工智能 安全
2025全球AI攻防挑战赛:巅峰对决,守护未来数字身份安全!| 阿里云天池平台赛事推荐丨云工开物
2025全球AI攻防挑战赛由多家顶尖机构联合主办,旨在应对AI时代安全威胁。赛事设置三大战场、六大命题,涵盖视觉、动态交互与声纹认证,推动动态攻防技术革新。参赛者将有机会获得百万奖金及与顶尖专家交流的平台。立即组队,共筑AI安全防线!
|
测试技术 网络安全
支付宝接口代签约失败排查指南
【11月更文挑战第5天】支付宝接口代签约失败排查指南:检查参数信息(必填参数、格式、业务合理性)、确认签约状态和权限(商户状态、应用权限、用户授权)、验证网络和系统环境(网络连接、系统兼容性、服务器配置)、检查异步通知设置(通知地址、处理逻辑)、查看官方文档和错误码、联系支付宝客服。
1243 14