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 ...
随机推荐
- Cacti的基本安装配置
////////////////////cacti///////////////////////////常用的监控软件有:cacti.nagios.zabbix等 cacti 重图形.有数据历史.需要 ...
- vue组件化开发-vuex状态管理库
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...
- javascript练习题(2):变量作用域
1. 外层变量在内部可以找到,反之找不到 以下看个案例: var a=10; function aaa(){ alert(a); } function bbb(){ var a=20; aaa(); ...
- 负载均衡之HTTP重定向
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38014581 由于目前现有网络的各个核心部分随着业务量的提高,访问量和数据流量的快速增长,其 ...
- DDOS 攻击工具
DDOS 攻击工具 使用github上的DDOS攻击工具 https://github.com/Ha3MrX/DDos-Attack 将python脚本拷贝到主机,使用 chmod +x ddos- ...
- 关于inline函数
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4975474.html 今天我在优化公司项目代码的过程中,借助了Intel的VTune ...
- CrateDB——全文搜索使用的是lucene,尚不知其底层实现
CrateDB: The fast, scalable, easy to use SQL database with native full text search https://crate.io ...
- com.itnba.maya.domel.Diaoyantimu_$$_javassist_1 cannot be cast to javassist.util.proxy.Proxy错误问题解决方法
控制台报错显示: com.itnba.maya.domel.Diaoyantimu_$$_javassist_1 cannot be cast to javassist.util.proxy.Prox ...
- 一次完整的HTTP请求
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接,TCP的三次握手 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服 ...
- spring 多数据源动态切换
理解spring动态切换数据源,需要对spring具有一定的了解 工作中经常遇到读写分离,数据源切换的问题,那么以下是本作者实际工作中编写的代码 与大家分享一下! 1.定义注解 DataSource ...