题目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. V8 下的垃圾回收机制

    V8 实现了准确式 GC,GC 算法采用了分代式垃圾回收机制.因此,V8 将内存(堆)分为新生代和老生代两部分. 1.新生代算法 新生代中的对象一般存活时间较短,使用 Scavenge GC 算法. ...

  2. MySql之存储过程的使用

    一:创建存储过程 1:简单存储过程 CREATE PROCEDURE 存储过程名() BEGIN SQL操作 END; 2:使用参数的存储过程 CREATE PROCEDURE 存储过程名(IN in ...

  3. logstash 学习小记

    logstash 学习小记 标签(空格分隔): 日志收集 Introduce Logstash is a tool for managing events and logs. You can use ...

  4. python3用BeautifulSoup抓取id='xiaodeng',且正则包含‘elsie’的标签

    # -*- coding:utf-8 -*- #python 2.7 #XiaoDeng #http://tieba.baidu.com/p/2460150866 #使用多个指定名字的参数可以同时过滤 ...

  5. linux下open和fopen的区别

    二者返回值不同. fopen可以指定宽字符和ASCI.

  6. 一篇文章让你读懂iOS和Android的历史起源

    智能手机虽说是移动电话,但我们完全可以将其作为小型化的电脑来思考.这样一来也能够显示出智能手机OS的高性能.我们首先一起来回顾下智能手机OS的历史. OS的黎明期 其实在很早之前就已经有这样的想法,即 ...

  7. Android 得到照片位置信息

    目前Android SDK定义的Tag有:TAG_DATETIME    时间日期TAG_FLASH   闪光灯TAG_GPS_LATITUDE   纬度TAG_GPS_LATITUDE_REF  纬 ...

  8. 菜鸟教程之工具使用(五)——JRebel与Windows服务的Tomcat集成

    之前写过一篇Tomcat借助JRebel支持热部署的文章——<借助JRebel使Tomcat支持热部署>.介绍的是在开发.测试环境中的配置,但是正式的部署环境,我们不会通过命令行来启动To ...

  9. [Big Data - Kafka] Kafka剖析(一):Kafka背景及架构介绍

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  10. Java编程的逻辑 (86) - 动态代理

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...