1139. First Contact (30)[快乐模拟]

1. 原题: https://www.patest.cn/contests/pat-a-practise/1139

2. 思路:

题意:快乐模拟类。
给出A, B,找出A的同性朋友C和B的同性朋友D。
其中C和D认识,输出C,D。
思路:
读入数据时,用邻接表记录A的同性朋友。
同时,标记两个人联系为true。
查询时,两次循环即可。
注意:
1)id可能为-0000,读入数据按字符串处理。
2)C和D不能同一人。

3. 源码:

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

struct Node
{
	bool operator< (const Node &b)
	{
		if (x == b.x)
			return (y < b.y);
		else
			return (x < b.x);
	}
	Node(){}
	Node(int a, int b) :x(a), y(b){}
	int x, y;
};

int N, M, K, ta, tb;
const int Max = 10000;
vector<int> G[Max];
bool H[Max][Max];

void process(int x, int y);

int main()
{
	//freopen("in.txt", "r", stdin);
	scanf("%d %d", &N, &M);

	int x, y;
	char sa[10], sb[10];
	for (int i = 0; i < M; i++)
	{
		scanf("%s %s", sa, sb);
		x = abs(atoi(sa));
		y = abs(atoi(sb));
		if (sa[0] == '-' && sb[0] == '-')
		{
			G[x].push_back(y);
			G[y].push_back(x);
		}
		else if (sa[0] != '-' && sb[0] != '-')
		{
			G[x].push_back(y);
			G[y].push_back(x);
		}

		H[x][y] = H[y][x] = true;
	}

	scanf("%d", &K);
	for (int i = 0; i < K; i++)
	{
		scanf("%s %s", sa, sb);
		x = abs(atoi(sa));
		y = abs(atoi(sb));

		process(x, y);
	}

	return 0;
}

void process(int x, int y)
{
	if (G[x].size() < 1 || G[y].size() < 1)
	{
		printf("0\n");
		return;
	}

	vector<Node> re;
	for (int i = 0; i < G[x].size(); i++)
	{
		int c = G[x][i], d;
		for (int j = 0; j < G[y].size(); j++)
		{
			d = G[y][j];
			if (c == y || d == x) //排除同一个人的情况
				continue;
			if (H[c][d])
			{
				re.push_back(Node(c, d) );
			}
		}
	}

	sort(re.begin(), re.end());
	printf("%d\n", re.size());
	for (int i = 0; i < re.size(); i++)
	{
		printf("%04d %04d\n", re[i].x, re[i].y);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值