题目https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312

题意:

有m对朋友关系,每个人用4为数字的编号表示,如果是负数表示这是女生。

给定k个查询,对于要查的人a和b,问有多少对朋友(c,d)使得c和a是同性,d和b是同性,且c和d是朋友。

思路:

枚举a的所有同性朋友,枚举b的所有同性朋友,看他们是不是朋友。

首先用了map离散化了一下给了个id,可能是这里搞来搞去T了最后一组数据。

实际上简单点可以直接用c++ 11标准里的stoi来做,感觉PAT老是碰到这个函数。

由于标号是唯一的,所以一个人只需要存储他的同性朋友就可以了。

开bool的话二维的1e5也是开的下的。

要注意枚举的时候如果直接碰到了a或b都要跳过。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, m, k;
int id = ;
bool fri[][];
vector<int>samefri[]; struct node{
int c, d;
node(){
}
node(int _c, int _d){
c = _c;
d = _d;
}
}; bool cmp(node a, node b)
{
if(a.c == b.c)return a.d < b.d;
else return a.c < b.c;
} int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++){
string a, b;
cin>>a>>b;
int first = abs(stoi(a));
int second = abs(stoi(b)); fri[first][second] = true;
fri[second][first] = true;
if(a.length() == b.length()){
samefri[first].push_back(second);
samefri[second].push_back(first);
}
} scanf("%d", &k);
while(k--){
string a, b;
cin>>a>>b; int cnt = ;
vector<node>ans;
int first = abs(stoi(a)), second = abs(stoi(b));
for(int i = ; i < samefri[first].size(); i++){
int c = samefri[first][i];
if(c == second)continue;
for(int j = ; j < samefri[second].size(); j++){
int d = samefri[second][j];
if(d == first)continue;
if(fri[c][d]){
cnt++;
ans.push_back(node(c, d));
//cout<<a<<" "<<b<<endl<<endl;
}
}
} printf("%d\n", cnt);
sort(ans.begin(), ans.end(), cmp);
for(int i = ; i < cnt; i++){
printf("%04d %04d\n", ans[i].c, ans[i].d);
}
}
return ;
}

PAT甲级1139 First Contact的更多相关文章

  1. PAT 1139 First Contact[难][模拟]

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  2. pat甲级1139

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  3. PAT甲级——A1139 First Contact【30】

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  4. pat advanced 1139. First Contact (30)

    题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...

  5. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

  6. PAT甲级考前整理(2019年3月备考)之三,持续更新中.....

    PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...

  7. PAT甲级满分攻略|记一次考试经历

    一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

随机推荐

  1. 〖Android〗从Android Studio转为Eclipse开发项目运行程序闪退的解决方法

    很久没有撸Android App开发了- 最近把一个月前通过反编译.二次修改的Android SSHD项目进行简单修改一下: 突然发现迁移项目时,报了一个错误,同时还出现了闪退情况: - ::): t ...

  2. 使用SpringBoot Admin监控SpringCloud微服务

    spring-boot admin的github地址:https://github.com/codecentric/spring-boot-admin 本文基于SpringCloud的环境和配置上增加 ...

  3. redis-desktop-manager 的简单使用

    1:安装比较简单,所有软件几乎都一样(下载.安装)我就从安装好后,怎么玩记录吧!如下图,双击对应的图标就能打开此软件了 2-1:连接redis服务器的方式之一——导入对应的redis信息 连接配置的样 ...

  4. 深入分析java线程池的实现原理(转载)

    前言 线程是稀缺资源,如果被无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池对线程进行统一分配.调优和监控,有以下好处: 1.降低资源消耗: 2.提高响应速度: 3.提高线程的 ...

  5. JS 动态生成JSON对象

    JS 动态生成JSON对象,一般用到JSON传递参数的时候,会用到. function onGeneratedRow(columnsResult) { var jsonData = {}; colum ...

  6. DUAL PORT RAM应用实例

    作者:桂. 时间:2018-05-14  12:11:00 链接:http://www.cnblogs.com/xingshansi/p/9035522.html 前言 主要是Xilinx 常用模块汇 ...

  7. Java并发之线程池ThreadPoolExecutor源码分析学习

    线程池学习 以下所有内容以及源码分析都是基于JDK1.8的,请知悉. 我写博客就真的比较没有顺序了,这可能跟我的学习方式有关,我自己也觉得这样挺不好的,但是没办法说服自己去改变,所以也只能这样想到什么 ...

  8. SQL数据类型和C#数据类型间的转换

    今天看到SQL数据类型和C#数据类型间的转换,前人留下的. <?xml version="1.0" encoding="utf-8" ?> < ...

  9. 关于java线程、进程的一些问题

    1.多核硬件上,java中同一个进程的多个线程可以运行在不同的CPU上么? 应该是可以的,在eclipse上面跑一个模拟程序,一个死循环的线程可以占用系统(4核,Win7)%的CPU,4个这样的线程刚 ...

  10. lua -- 物品的配置文件,表的形式保存

    do local goodsprop= { RECORDS={ []={ BuyGold= , RecoverPrice= , Value= , Param2= , UseTimesPerDay= , ...