pat advanced 1139. First Contact (30)
- 解法暴力
- 因为有 0000, -0000 这样的数据,所以用字符串处理
- 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
//解法暴力
//因为有 0000, -0000 这样的数据,所以用字符串处理
//同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue
int n, m;
unordered_map<string, unordered_set<string>> relations;
bool is_same_gender(string id1, string id2)
{
if ((id1[0] == '-' && id2[0] == '-') || (id1[0] != '-' && id2[0] != '-')) return true;
return false;
}
struct result
{
string id1;
string id2;
result(string _x, string _y)
{
if (_x[0] == '-') id1 = _x.substr(1);
else id1 = _x;
if (_y[0] == '-') id2 = _y.substr(1);
else id2 = _y;
}
void print()
{
cout << id1 << " " << id2 << endl;
}
bool operator<(const result&r)const
{
return id1 == r.id1 ? id2 < r.id2: id1 < r.id1 ;
}
};
int main()
{
while (scanf("%d %d", &n, &m) != EOF)
{
char id1[6], id2[6];
relations.clear();
while (m--)
{
scanf("%s %s", id1, id2);
relations[id1].insert(id2);
relations[id2].insert(id1);
}
int q;
scanf("%d", &q);
while (q--)
{
vector<result> res; res.clear();
scanf("%s %s", id1, id2);
for (unordered_set<string>::iterator i = relations[id1].begin(); i != relations[id1].end(); i++)
{
if (!is_same_gender(id1, (*i)) || (*i) == string(id2)) continue;
for (unordered_set<string>::iterator j = relations[id2].begin(); j != relations[id2].end(); j++)
{
if (!is_same_gender(id2, (*j)) || (*j) == string(id1)) continue;
if (relations[(*i)].find(*j) != relations[(*i)].end())
{
res.push_back(result((*i), (*j)));
}
}
}
sort(res.begin(), res.end());
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++)
res[i].print();
}
}
return 0;
}
pat advanced 1139. First Contact (30)的更多相关文章
- PAT 1139 First Contact[难][模拟]
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- PAT甲级1139 First Contact
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT Advanced 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT Advanced 1072 Gas Station (30) [Dijkstra算法]
题目 A gas station has to be built at such a location that the minimum distance between the station an ...
- PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]
题目 Input our current position and a destination, an online map can recommend several paths. Now your ...
- PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
随机推荐
- Codeforces Round #519 题解
A. Elections 题意概述 给出 \(a_1, \ldots, a_n\),求最小的 \(k (k \ge \max a_i)\), 使得 \(\sum_{i=1}^n a_i < \s ...
- Linux使用imagemagick的convert命令压缩图片、节省服务器空间
一.安装: sudo apt-get install imagemagick 二.说明 imagemagick的命令convert可以完成此任务,其参数-resize用来改变图片尺寸,可以直接指定像素 ...
- Ubuntu django+nginx 搭建python web服务器文件日志
uwsgi 配置文件 [uwsgi] http-socket = 127.0.0.1:8080 # 项目目录 chdir=/home/ubuntu/mkweb # 指定项目的application m ...
- Sublime Text 之运行 js 方法[2015-5-6更新mac下执行js]
昨天说完<Sublime Text 2 绿化与汉化 [Windows篇]>,今天我们来说说怎么用st直接运行 js 吧.群里的小伙伴一直对我的 ST 能直接运行js感到非常好奇,今天我就公 ...
- [原] eclipse 无法找到 run as junit
碰见这个问题,折磨我好一下! 问题根源和解决方式 第一,保证有junit jar包,基本不会犯这错误: 第二,保证你这个类是Source可编译文件,要是这个类在普通文件夹下,工程是不会编译它的,也就找 ...
- iOS动画1 — UIView动画
iOS动画基础是Core Animation核心动画.Core Animation是iOS平台上负责图形渲染与动画的基础设施.由于核心动画的实现比较复杂,苹果提供了实现简单动画的接口—UIView动画 ...
- gentoo系统安装
1. Gentoo常用镜像 ===>http://www.linuxsir.org/bbs/thread263600.html 2. 安装方式1 http://www.linuxidc.co ...
- Mac安装MAT报错问题
安装mat报错,提示在/.eclipse/1528649425_macosx_cocoa_x86_64/configuration/1539332382530.log路径下查看错误日志, 原因是/pr ...
- wpf 如果列表加载超多数据变的卡顿时,使用VirtualizingStackPanel
如果列表加载超多数据变的卡顿时 <ListBox > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Virt ...
- ECS Navicat for MySQL远程连接报10038的错误
解决问题时,建议先在阿里云设置好数据库访问的白名单,把自己的IP地址填进去 问题现象 Navicat for MySQL访问远程mysql数据库,出现报错,显示“2003- Can't connect ...