1139 First Contact
题意:给出n个人,m对朋友关系,其中带负号的表示女孩。然后给出k对查询a,b,要求找出所有a的同性别好友c,以及b的同性别好友d,且c和d又是好友关系。输出所有满足条件的c和d,按c的升序输出,若c编号相同则按d的升序输出。
思路:其实不难,30分的题有点被唬住了。
对于每一个查询a,b,先分别找出a同性好友friend[a],b的同性好友friend[b],然后在friend[a]和friend[b]中找出具有朋友关系的c,d即可。注意,根据题意,-0000和0000是不一样的,若以int类型数据读入,则均表示为0,从而无法区别性别,因此读入数据应该以字符串的类型读入。两者性别是否一致可通过其字符串的长度是否一致来区别。最后,若a,b为同性时,存在a的好友就是b,或b的好友就是a的特殊情况,应该排除掉。因为a要向b联系必须通过两个中间人!
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <map>
#include <set>
#include <vector>
using namespace std;
unordered_map<int,unordered_map<int,bool>> mp;//表示朋友关系的映射
map<int,set<int>> friendOfBoy,friendOfGirl;
int main()
{
//freopen("pat.txt","r",stdin);
int n,m,k;
scanf("%d%d",&n,&m);
],b[];
;i<m;i++){
scanf("%s%s",a,b);
int u=abs(atoi(a)), v=abs(atoi(b));
mp[v][u]=mp[u][v]=true;
if(strlen(a)==strlen(b)){
]=='-'){
friendOfGirl[u].insert(v);
friendOfGirl[v].insert(u);
}else{
friendOfBoy[u].insert(v);
friendOfBoy[v].insert(u);
}
}
}
scanf("%d",&k);
;i<k;i++){
set<int> friendA,friendB;
vector<pair<int,int>> ans;
scanf("%s%s",a,b);
int u=abs(atoi(a)), v=abs(atoi(b));
if(strlen(a)==strlen(b)){
]=='-'){
friendA=friendOfGirl[u];
friendB=friendOfGirl[v];
}else{
friendA=friendOfBoy[u];
friendB=friendOfBoy[v];
}
}]=='-'){
friendA=friendOfGirl[u];
friendB=friendOfBoy[v];
}else{
friendA=friendOfBoy[u];
friendB=friendOfGirl[v];
}
for(auto p:friendA){
for(auto q:friendB){
if(p==v || q==u) continue;
if(mp[p][q]) ans.push_back(pair<int,int>(p,q));
}
}
printf("%d\n",ans.size());
for(auto it:ans)
printf("%04d %04d\n",it.first,it.second);
}
;
}
1139 First Contact的更多相关文章
- PAT 1139 First Contact[难][模拟]
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- 1139 First Contact(30 分)
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- PAT 1139 First Contact
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- PAT甲级1139 First Contact
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...
- pat advanced 1139. First Contact (30)
题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...
- 1139 First Contact PAT (Advanced Level)
原题链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 测试点分析: 首先来分析一下测试 ...
- pat甲级1139
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- PTA 1139 1138 1137 1136
PAT 1139 1138 1137 1136 一个月不写题,有点生疏..脑子跟不上手速,还可以啦,反正今天很开心. PAT 1139 First Contact 18/30 找个时间再修bug 23 ...
- PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟
1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...
随机推荐
- Spring简洁版总结
一:为什么使用spring? spring泉眼,最好的水,在企业开发中,是业务层最好的框架 spring优点是什么? 1.低侵入,低耦合 2.方便集成其他框架 3.降低javaee开发难度 4.spr ...
- docker定义数据卷及数据卷的备份恢复
前言:生产环境中使用docker时,往往需要对数据进行持久化(只有把容器导出为镜像,才能够保存写的数据,否则容器删除或者停止,所有数据都会没有),或者需要在多个容器之间进行数据共享,这必然涉及容器的数 ...
- vmware增加共享文件夹
增加共享文件夹 VMWare提供共享文件夹功能.前提是在虚拟机中安装VMware tools 1. 安装VMware tools 会自动在虚拟机中的/media/VMware Tools/中有个压缩包 ...
- java中的char类型所占空间
java中统一使用unicode编码,所以每个字符都是2个字节16位.unicode包括中文,所以对String类计算长度的时候,一个中文和一个英文都是一个长度.String voice = &quo ...
- spring3: 对JDBC的支持 之 关系数据库操作对象化
7.3.1 概述 所谓关系数据库对象化其实就是用面向对象方式表示关系数据库操作,从而可以复用. Spring JDBC框架将数据库操作封装为一个RdbmsOperation,该对象是线程安全的.可复 ...
- uva 11825 巧妙地子集枚举方法
https://vjudge.net/problem/UVA-11825 题目大意,有n台服务器,有n种服务,每台服务器都运行着所有的服务,一台服务器可以被攻击一次其中的一种服务,当你选择攻击某台服务 ...
- 使用VS自带的工具分析.NET程序的性能
(转自:http://www.cnblogs.com/DebugLZQ/archive/2012/07/10/2585245.html) 这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我 ...
- S3C2440启动方式
不管S3C2440的启动设备是什么,它都是从0x0000 0000地址开始执行程序的,所不同的是地址的映射不一样.基于S3C2440的嵌入式系统上电之后,需要首选选择启动设备,2440的启动方式选择是 ...
- UI-了解ISO
1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...
- JavaScript: Where to Insert JavaScript and output
1.Javescript in <head> <!DOCTYPE html> <html> <head> <script> function ...