PAT 1139 First Contact (30point(s))(解题记录)

I don’t want to say more about this question…
Note that the ID can be -0000!! So we should store ID in a string instead of int valid.
I have no time to simply my code,so I’m sorry that it would be a little tedious.

#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string.h>
#include<stack>
#include <unordered_map>
#include <queue>

using namespace std;
const int MAXN = 330;
int G[MAXN][MAXN];
map<string, int> Index;

queue<string> A;
queue<string> B;

int N, M, K;

void solve(string u,string v)
{
	int cnt = 0;
	string uu, vv;
	if (u.size() == 4) uu = u;
	else uu = u.substr(1, 4);
	if (v.size() == 4) vv = v;
	else vv = v.substr(1, 4);
	
	int ui = Index[uu], vi = Index[vv];
	if (u.size()==v.size()) { //uv同性
		for (auto a : Index)
		{
			if (a.first != vv && G[ui][a.second] == 1) {//a是u的同性朋友
				for (auto b : Index)
				{
					if (G[a.second][b.second] == 1&&b.first!=uu)//b是a的同性朋友
					{
						if (G[b.second][vi] == 1) {
							++cnt;
							A.push(a.first);
							B.push(b.first);
						}
					}
				}
			}
		}
		cout << cnt << endl;
		while (!A.empty())
		{
			cout << A.front() << " " << B.front() << endl;
			A.pop(); B.pop();
		}
	}
	else {
		for (auto a : Index)
		{
			if (a.first != vv && G[ui][a.second] == 1) {//a是u的同性朋友
				for (auto b : Index)
				{
					if (b.first != vv && G[a.second][b.second] == 2)//b是a的异性朋友
					{
						if (G[b.second][vi] == 1) {
							++cnt;
							A.push(a.first);
							B.push(b.first);
						}
					}
				}
			}
		}
		cout << cnt << endl;
		while (!A.empty())
		{
			cout << A.front() << " " << B.front() << endl;
			A.pop(); B.pop();
		}
	}
}

int main() {

	memset(G, 0, sizeof(G));
	cin >> N >> M;
	
	string u, v,uu,vv;
	int ui, vi;
	
	for (int i = 0; i < M; i++)
	{
		cin >> u >> v;

		if (u.size() == 4) {
			uu = u;
		}else {
			uu = u.substr(1, 4);
		}

		if (v.size() == 4) {
			vv = v;
		}
		else {
			vv = v.substr(1, 4);
		}

		if (Index.count(uu)) ui = Index[uu];
		else 
		{
			ui = Index.size();
			Index[uu] = ui;
			   
		}

		if (Index.count(vv)) vi = Index[vv];
		else {
			vi = Index.size();
			Index[vv] = vi;
		}
		
		if (u.size()==v.size()) {
			G[ui][vi] = 1;
			G[vi][ui] = 1;
		}
		else {
			G[ui][vi] = 2;
			G[vi][ui] = 2;
		}


	}
	
	cin >> K;
	while (K--)
	{
		cin >> u >> v;
		
		if (u.size() == 4) uu = u;
		else uu = u.substr(1, 4);
		
		if (v.size() == 4) vv = v;
		else vv = v.substr(1, 4);
		
		if (!Index.count(uu) || !Index.count(vv)) {
			cout << 0 << endl; 
			continue;
		}
		solve(u, v);

	}

	return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值