PAT甲组1139 First Contact思路解析和代码

A1139

题目链接

个人思路

  • 由于数据量是1w,如果用邻接表邻接矩阵存储总觉得会超限
  • 声明两个结构体,person存储每个人的id,性别,vector存储朋友id,便于遍历;Helper存储两个中间人的id
  • 输入数据,初始化每个人的变量
  • 输入一对情侣A喜欢B,在第一个人A的朋友中寻找同性朋友C,在C的朋友中寻找与B同性的朋友D,在D的朋友中如果能找到B,将C,D存入ans中,遍历结束输出答案即可

注意:

  • 依照题意,是存在AB同性的情况的哈哈哈,所以if条件为A同性,B同性来寻找C,D,而不能明确说明D一定与A异性
  • 明确要找的是中间人:在筛选每个人朋友中,要注意不能在A的朋友中直接找到B,不能在C的朋友中直接找到A和B
  • 注意由于-0000与+0000在int中都是0,无法判断性别,因此最开始要用字符串读入来判断性别
  • 柳神使用10000 * p1 + p2的哈希函数,也是值得学习的!用hash替代二维4位数数组是个好方法

个人思路代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int N, M, K;
struct Person{
	int id;
	int gender = 1;//0:女,1:男 
	vector<int> mate;
}people[maxn];
struct Helper{
	int firid;
	int secid;
	Helper(){}
	Helper(int fid, int sid)
	{
		firid = fid;
		secid = sid;
	}
};
vector<Helper> ans;
unordered_map<int ,bool> gendermp;
void findFriend(int A, int B)
{
	for(int i = 0; i < people[A].mate.size(); ++i)//遍历A的朋友 
	{
		int pid = people[A].mate[i];//A的朋友id 
		if(people[pid].gender == people[A].gender && pid != B)//与A同性的朋友C 
		{
			for(int j = 0; j < people[pid].mate.size(); ++j)//遍历C的朋友 
			{
				int qid = people[pid].mate[j];//C的朋友id 
				if(people[qid].gender == people[B].gender && qid != B && qid != A)//与A,C异性,与B同性的朋友D 
				{
					for(int k = 0; k < people[qid].mate.size(); ++k)//遍历D的朋友 
					{
						int rid = people[qid].mate[k];//D的朋友id 
						if(people[rid].id == B)//找到B 
						{
							ans.push_back(Helper(pid, qid));
						}
					}
				}
			}
		}
	}
}
bool cmp(Helper h1, Helper h2)
{
	if(h1.firid != h2.firid)
		return h1.firid < h2.firid;
	else
		return h1.secid < h2.secid;
}
int main(int argc, char *argv[]) {
	scanf("%d%d", &N, &M);
	//getchar();
	for(int i = 0; i < M; ++i)
	{
		char s1[10], s2[10];
		scanf(" %s %s", s1, s2);
		int p1, p2;
		sscanf(s1, "%d", &p1);
		sscanf(s2, "%d", &p2);

		if(s1[0] == '-')
		{
			p1 *= -1;
			people[p1].gender = 0;
		}
		if(s2[0] == '-')
		{
			p2 *= -1;
			people[p2].gender = 0;
		}
		people[p1].id = p1;
		people[p1].mate.push_back(p2);
		people[p2].id = p2;
		people[p2].mate.push_back(p1);
	}
	scanf("%d", &K);
	while(K--)
	{
		int A, B;
		ans.clear();
		scanf("%d%d", &A, &B);
		A = abs(A);
		B = abs(B);
		findFriend(A, B);
		printf("%d\n", ans.size());
		sort(ans.begin(), ans.end(), cmp);
		for(int i = 0; i < ans.size(); ++i)
		{
			Helper h = ans[i];
			printf("%04d %04d\n", h.firid, h.secid);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值