PAT A1139 First Contact (30point(s))

Scroll Down

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.

题意

A和B是恋人,但A不会直接与B传递信息,而是告诉A的一个同性朋友C,然后C告诉一个认识B且是B的同性朋友的朋友,传递链为A->C->D->B,A与C和B与D为同性。给出A和B求C和D。

解答

每输入一组数据a和b,就把b标记为a的朋友,a标记为b的朋友,这里用unordered_map存储,因为不需要有序,而且使用map会超时。查找时直接查找哈希表,首先看是否存在这个人,如果不存在直接输出0;如果存在,则查找该人的所有朋友,同理,再查找朋友的朋友,如果满足关系链,就保存结果。最后将结果排序打印,打印id时要格式化为%04d,否则小于四位数的数打印出来不足四位,这也是做PAT时容易犯的错误。
本题有两个坑点需要注意:

  1. 可能存在A->B->C->B的关系链,但不符合题意,因此查找A的朋友时,要过滤掉B,查找B的朋友,要过滤掉A。
  2. 题目并没说id号不能全0,因此存在0000和-0000这种情况,如果用int类型存储id,那这两个id会被视为同个id,造成结果出错,因此要使用string或char*类型来存储id
#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;
bool cmp1(pair<int, int> p1, pair<int, int> p2)
{
	if (p1.first != p2.first) return p1.first < p2.first;
	return p1.second < p2.second;
}
int main()
{
	unordered_map<string, unordered_map<string, bool>> rel;
	int peopleNum, relNum;
	string a, b;
	cin >> peopleNum >> relNum;
	for (int i = 0; i < relNum; i++) {
		cin >> a >> b;
		rel[a][b] = true;
		rel[b][a] = true;
	}
	int testNum;
	cin >> testNum;
	for (int i = 0; i < testNum; i++) {
		cin >> a >> b;
		// a没朋友
		if (rel.find(a) == rel.end() || rel.find(b) == rel.end()) {
			printf("0\n");
			continue;
		}
		unordered_map<string, bool> aF = rel[a];
		vector<pair<int, int>> ans;
		for (auto it = aF.begin(); it != aF.end(); it++) {
			if (((it->first[0]=='-' && a[0]=='-') ||
				(it->first[0] != '-' && a[0] != '-')) 
				&& it->first != b) { // 和a同性别
				unordered_map<string, bool> itF = rel[it->first];
				for (auto it2 = itF.begin(); it2 != itF.end(); it2++) {
					if (rel[it2->first].find(b) != rel[it2->first].end()
						&& ((it2->first[0] == '-' && b[0] == '-') ||
						(it2->first[0] != '-' && b[0] != '-'))
						&& it2->first != a) {
						int f1 = stoi(it->first);
						int f2 = stoi(it2->first);
						ans.push_back({
							f1 > 0 ? f1 : -f1,
							f2 > 0 ? f2 : -f2
							});
					}
				}
			}
		}
		printf("%u\n", ans.size());
		sort(ans.begin(), ans.end(), cmp1);
		for (auto it = ans.begin(); it != ans.end(); it++) {
			printf("%04d %04d\n", it->first, it->second);
		}
	}
	return 0;
}