题目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. Python根据多个空格Split字符串

    下面的String: 1 沪1 上海市 1850 1350 400 1300 1186/1644(嘉定约100,松江69 奉贤68 2007年上海常住人口1858万人,户籍人口1378.86万人,来沪 ...

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

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

  3. 判断一棵二叉树是否为AVL树

    思路:AVL树是高度平衡的二叉搜索树,这里为了清晰说明,分别判断是否为搜索树,是否为平衡树. struct TreeNode { struct TreeNode *left; struct TreeN ...

  4. npm错误:Error: listen EADDRNOTAVAIL

    错误 Error: listen EADDRNOTAVAIL 127.0.0.1:8080 有两种情况 8080端口被绑定了 地址错误 Error: getaddrinfo ENOTFOUND 域名错 ...

  5. centos 7 配置tomcat开机启动

    1. tomcat 需要增加一个pid文件 在tomca/bin 目录下面,增加 setenv.sh 配置,catalina.sh启动的时候会调用,同时配置java内存参数. #add tomcat ...

  6. Java多线程系列——信号量:Semaphore

    简介 信号量为多线程协作提供了更为强大的控制方法.也可以说,信号量是对锁的扩展.无论是内部锁 synchronized 还是重入锁 ReentrantLock,一次都只允许一个线程访问一个资源,而信号 ...

  7. Unsafe 学习和源码阅读

    在代码中获取 Unsafe 对象的方法: // 在 AtomicInteger 里面是这么用的private static final Unsafe unsafe = Unsafe.getUnsafe ...

  8. AI金融知识自学偏量化方向-目录0

    前提: 统计学习(统计分析)和机器学习之间的区别 金融公司采用机器学习技术及招募相关人才 了解不同类型的机器学习 有监督学习 vs 无监督学习 迭代和评估 偏差方差权衡 结合有监督学习和无监督学习(半 ...

  9. 关于Unity中ARPG游戏人物移动(专题十一)

    ARPG:动作型角色扮演类游戏 大多数的ARPG游戏都是使用摇杆操作,以第三人称摄像机的方式来跟随主角,实际上人物只走八个方向,上,下,左,右,左上,左下,右下,右上 控制角色移动的思路1: 在ARP ...

  10. easyradius隆重发布ROS API计费接口,支持ROS 3.3以上版本,实现简单快捷的ROS宽带计费系统云端版

    easyradius对接ROS也是使用的是ROS的api接口,即您要开启 /ip /service api服务,不了解api接口的,可以自己百度噢 开启API,就到easyradius配置通讯接口,具 ...