1139. First Contact (30)-PAT甲级真题 (字符串哈希表构图)

题意

给定结点数n、边数m,然后给出m个端点v1、v2。根据结点字符串首位是否为-确定端点为男生还是女生。然后询问q次,每次给出想交朋友的v1、v2,要求找到组合{v3, v4}使得v3是v1的同性朋友,v4是v2的同性朋友,同时v3是v4的朋友,输出可能性数量,并依次输出,输出时若存在-,则需要去掉。

思路1 - 哈希表

  • 由于输入数字在输出时要取abs,且可能存在男孩子0000女孩子-0000的冲突,于是决定用哈希表unordered_map<string, unordered_map<string, bool>>存图。
  • 构图存边rec[a][b] = rec[b][a] = true
  • 初始化ans数组用于后面存组合,注意同性孩子交朋友和异性交朋友在此逻辑相同。
  • 内层循环寻找目标,注意v3不能是v2,v4不能是v1这一个隐含条件。测试点3、测试点4、测试点5在这里卡住了,加上i.first != bj.first != a这两个条件就AC了。但感觉用这种情况卡是有问题的,因为这种条件说明A、B已经是朋友了,而题意为A、B非朋友。
  • 加上条件AC,4kB + 100ms。见解法1。

思路2 - 邻接表法

  • 由于没有考虑A、B已经是朋友的情况,被后三个测试点卡住了,换用常规vector<vector<int>> rec(20001)存图(以为是编译器访问不存在边rec[v1][v2]时会自动添加边rec[v1][v2] = true其实是自动添加边rec[v1][v2] = false)。
  • 考虑到存在男孩子0000女孩子-0000的冲突,将男生散列到下标10000 - 19999,女生散列到下标0000 - 9999。即ia = stoi(a) + (a[0] == '-' ? 9999 : 10000), ib = stoi(b) + (b[0] == '-' ? 9999 : 10000);然后rec[ia].push_back(ib);rec[ib].push_back(ia);
  • 其余步骤同思路1,不过写到这儿考虑到了A、B已经是朋友的情况,加上条件,就AC了。
  • 2kB + 40ms。见解法2。
  • 虽然哈希表查找速度O(1),邻接表查找速度O(n),但是这里思路2却更快,不知道是测试点不规范,还是对哈希表的理解不深,有遗漏知识点。

总结

  • 编译器访问unordered_map中不存在边rec[v1][v2]时会自动添加边rec[v1][v2] = false

解法1

#include<bits/stdc++.h>
using namespace std;
int n, m, q;
unordered_map<string, unordered_map<string, bool>> rec;
bool cmp(string a, string b){
	return ((a[0] == '-' && b[0] == '-') || (a[0] != '-' && b[0] != '-')) ? true : false;
}
int main(){
	cin>>n>>m;
	string a, b;
	while(m--){
		cin>>a>>b;
		rec[a][b] = rec[b][a] = true;
	}
	cin>>q;
	while(q--){
		cin>>a>>b;
		vector<pair<string, string>> ans;
		for(auto i : rec[a])
			if(i.first != b && i.second == true && cmp(a, i.first))
				for(auto j : rec[i.first])
					if(j.first != a && j.second == true && rec[j.first][b] == true && cmp(j.first, b))
						ans.push_back({(i.first[0] == '-') ? i.first.substr(1, i.first.size() - 1) \
						: i.first, (j.first[0] == '-') ? j.first.substr(1, j.first.size() - 1) : j.first});
		printf("%d\n", ans.size());
		sort(ans.begin(), ans.end());
		for(int i = 0; i < ans.size(); i ++)
			printf("%s %s\n", ans[i].first.c_str(), ans[i].second.c_str());
	}
	return 0;
} 

解法2

#include<bits/stdc++.h>
using namespace std;
int n, m, q, ia, ib;
string a, b;
vector<vector<int>> rec(20001);
bool cmp(int a, int b){
	return ((a >= 10000 && b >= 10000) || (a < 10000 && b < 10000)) ? true : false;
}
int main(){
	cin>>n>>m;
	while(m--){
		cin>>a>>b;
		ia = stoi(a) + (a[0] == '-' ? 9999 : 10000), ib = stoi(b) + (b[0] == '-' ? 9999 : 10000);
		rec[ia].push_back(ib);
		rec[ib].push_back(ia);
	}
	cin>>q;
	while(q--){
		cin>>a>>b;
		ia = stoi(a) + (a[0] == '-' ? 9999 : 10000), ib = stoi(b) + (b[0] == '-' ? 9999 : 10000);
		vector<pair<int, int>> ans;
		for(auto i : rec[ia])
			if(i != ib && cmp(ia, i))
				for(auto j : rec[i])
					if(j != ia && cmp(j, ib))
						for(auto k : rec[j])
							if(k == ib)
								ans.push_back({i >= 10000 ? i - 10000 : abs(i - 9999), j >= 10000 ? j - 10000 : abs(j - 9999)});
		printf("%d\n", ans.size());
		sort(ans.begin(), ans.end());
		for(int i = 0; i < ans.size(); i ++)
			printf("%04d %04d\n", ans[i].first, ans[i].second);
	}
	return 0;
} 

题目

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值