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


目录
相关文章
|
4天前
|
监控
某市河西区教育信息网被加入传播Worm.Win32.Viking.jr等的代码
某市河西区教育信息网被加入传播Worm.Win32.Viking.jr等的代码
【PAT甲级】1146 Topological Order
【PAT甲级】1146 Topological Order
58 0
|
存储 C++ Windows
【PAT甲级】1017 Queueing at Bank
【PAT甲级】1017 Queueing at Bank
44 0
|
算法框架/工具
【PAT甲级】1126 Eulerian Path
【PAT甲级】1126 Eulerian Path
58 0
【PAT甲级】1150 Travelling Salesman Problem
【PAT甲级】1150 Travelling Salesman Problem
48 0
|
JavaScript
美食杰项目 -- 菜品信息(五)
美食杰项目 -- 菜品信息(五)
美食杰项目 -- 菜品信息(五)
PAT甲级 1005. Spell It Right(20分)
PAT甲级 1005. Spell It Right(20分)
50 0
SAP HUM针对HU做货物移动后生成的物料凭证何处看相关的HU信息?
SAP HUM针对HU做货物移动后生成的物料凭证何处看相关的HU信息?
SAP HUM针对HU做货物移动后生成的物料凭证何处看相关的HU信息?
SAP HUM已知一个内部HU号码,如何得到HU号码?
SAP HUM已知一个内部HU号码,如何得到HU号码?