PAT甲级1139 First Contact
题目: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的更多相关文章
- PAT 1139 First Contact[难][模拟]
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- pat甲级1139
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- PAT甲级——A1139 First Contact【30】
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- pat advanced 1139. First Contact (30)
题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...
- PAT甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
随机推荐
- void java.lang.System.gc()
void java.lang.System.gc() Runs the garbage collector. Calling the gc method suggests that the Java ...
- Google 发布的15个 Android 性能优化典范
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- 浅析Sql Server参数化查询
说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优化一个CPU占用很高的复杂SQL语句时,我才突然意识到了参数化查询的重要性. 相信有很多开发者和我一样对于参数化查询认识比较模糊,没有引起足 ...
- 【C语言】数组名传递给函数,数组的sizeof变为4的原因
C语言中,数组名作为参数传递给函数时,退化为指针,sizeof对指针操作结果应该是4.例子如下: #include<iostream> using namespace std; void ...
- KeyTool 和 OpenSSL 相互转换 [转]
REM 生成自签名 CA 证书 REM Win32 OpenSSL REM http://slproweb.com/products/Win32OpenSSL.html REM How to crea ...
- Python list 常用方法总结
一,创建列表 只要把逗号分隔的不同的数据项使用方括号([ ])括起来即可 下标(角标,索引)从0开始,最后一个元素的下标可以写-1 list = ['1',‘2,‘3’] list = [] 空 ...
- python文件的基础操作
import os print(,'-')) print(os.getcwd()) print(,'-')) print(os.listdir()) print(,'-')) print(os.lis ...
- svn常见错误解决
Svn冲突导致锁住的解决方案:错误码:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interr ...
- SAP HANA S4 FI TABLE表结构
一.统一日记账的表 1)一个行项目表,存储所有应用的全部明细–迅速获得洞察力和扩展能力; 2)次级成本要素也变成了总账科目,统一入口维护和管理; 3)数据只需存储一次在一张表,不需要再做月末对账,如A ...
- Scala学习笔记(三):==,eq与equals的区别
== Scala中==与java中不同,它是比较值是否相等的,无论比较对象是否是相同类型 List(1, 2, 3) == List(1, 2, 3) //true 1==1.0//true equa ...