【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;
}


目录
相关文章
|
Python
python打印直角三角形
python打印直角三角形
274 3
跟我从0学Python——类的继承和多态
类的继承和多态 —— 面向对象编程的扩展与灵活性
|
负载均衡 前端开发 应用服务中间件
NGINX高可用之keepalived+nginx主从模式+主主模式配置实践
NGINX高可用之keepalived+nginx主从模式+主主模式配置实践
1505 1
|
缓存 数据挖掘 BI
面试官问你:日亿万级请求日志收集如何不影响主业务?你怎么回复
数据收集 上篇详细讨论了写缓存的架构解决方案,它虽然可以减少数据库写操作的压力,但也存在一些不足。比如需要长期高频插入数据时,这个方案就无法满足,接下来将围绕这个问题逐步提出解决方案。
|
JavaScript 前端开发
Vue——04-01用$set给对象添加属性
用$set给对象添加属性
380 0
|
机器学习/深度学习 人工智能 JavaScript
C语言【23道】经典面试题【下】
C语言【23道】经典面试题【下】
182 0
|
1天前
|
云安全 监控 安全
|
7天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
844 5
|
12天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
1072 40