✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1139 First Contact (pintia.cn)
🔑中文翻译:第一次接触
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪
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
的性别相同,则两个朋友的性别必须也和他们相同。
保证每个人只有一种性别。
一组询问内,朋友对按第一个朋友的编号从小到大进行排序,第一个朋友的编号相同时,按第二个朋友的编号从小到大进行排序。
思路
-
这道题给定的人数范围是
300
以内,所以我们可以用邻接矩阵来存储给定的图。但是每个人用编号来表示,故需要先将每个人映射在1~300
的下标之间。用映射后的下标再去关系记录到邻接矩阵当中,并且用两个数组来记录每个人是男孩还是女孩。需要注意的是,女孩的编号为负号,需要先处理再去映射,同时也要另外用一个数组来记录下标在映射之前对应的编号是什么,方便后续查找。
-
当输入完信息后,需要对记录男孩和女孩的数组进性排序和去重,因为在输入时,每个编号都加到数组中,其中可能会存在重复的值。
-
查询给定两人的第一次联系的朋友,需要先去判断两人的性别再进行遍历。遍历时如果找到满足
A -> C -> D -> B
的答案,则将答案加入到答案数组中。 -
最后对答案进行排序,并输出最终的结果。
代码
#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;
}