题目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. grep 多行 正则匹配

    https://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep I ...

  2. CMD递归文件夹

    SET dir=%~dp0 SET /a cnt=0 echo dir is: %dir% cd /d %dir% for /R %dir% %%i in (*.apk) do ( set /a cn ...

  3. Redis server went away的解决方案

    Redis server went away出现的问题如下: 1.看redis服务是否启动,包括端口 2.看是否是服务器端的防火墙引起的,iptables和selinux 3.看是否是redis.co ...

  4. 12C -- DDL日志

    DDL日志和alert日志有相似的格式和行为.但是只包含DDL语句日志.oracle只是为数据库组件提供DDL日志,且需要将参数enable_ddl_logging设置为true. 在DDL日志中,每 ...

  5. C#_基础题1-10套答案

    one 1.用户输入一个整数,用if...else判断是偶数还是奇数             Console.WriteLine("请输入一个整数:");              ...

  6. 【R作图】lattice包,画多个分布柱形图,hist图纵轴转换为百分比

    一开始用lattice包,感觉在多元数据的可视化方面,确实做得非常好.各种函数,可以实现任何想要实现的展示. barchart(y ~ x) y对x的直方图 bwplot(y ~ x) 盒形图 den ...

  7. win8使用技巧

    windows 8操作系统相信大家已经不再陌生了,虽然正式版本还未发布,但不少朋友已经在使用微软事先推出的windows 消费者预览版,直白的说就是公测版,预览版是免费的,但仅可以使用一年,但其功能与 ...

  8. 实现A星算法

    [更新] 稍微将A*算法进行修正,使用BFS(按F值对open表排序),另外,新增评估函数,用来测量当前点到终点的线段上的随机某一点是否是墙或已访问结点,是的话返回1,否则返回0. function ...

  9. 【九天教您南方cass 9.1】 08 绘制等高线及对其处理

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取测量空间中. [点击索取cass教程]5元立得 (给客服说暗号:“ ...

  10. 系统垃圾清理利器CCleaner v5.30.6063绿色单文件版(增强版)

    系统垃圾清理利器CCleaner现已更新至v5.30.6063,此次更新为Edge.IE浏览器提供了更好的清理功能,更新了Windows Explorer MRU清理功能,同时改善了应用程序中的SSD ...