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.





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

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()) {
		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);
							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;