题目链接

  1. 解法暴力
  2. 因为有 0000, -0000 这样的数据,所以用字符串处理
  3. 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该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)的更多相关文章

  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 First Contact

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...

  3. 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 ...

  4. PAT (Advanced Level) 1057. Stack (30)

    树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

  5. PAT Advanced 1022 Digital Library (30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  6. PAT Advanced 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  7. 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 ...

  8. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

  9. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

    题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...

随机推荐

  1. python学习笔记6--操作redis

    一.redis操作 import redis r=redis.Redis(host='211.149.218.16',port=6379,password='123456',db=2) r.set(' ...

  2. elementUI 通用确认框

    Util.vue <script> import VueResource from 'vue-resource' function confirm(_this, operate, fun) ...

  3. (64位)本体学习程序(ontoEnrich)系统使用说明文档

    系统运行:文件夹system下,可执行文件ontoEnrichment 概念学习 --------------------------------------------------------1.简 ...

  4. CSS reset--(来自网易)

    /* reset */ html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,capti ...

  5. TED_Topic9:How we're priming some kids for college — and others for prison

    Alice Goffman In the United States, two institutions guide teenagers on the journey to adulthood: co ...

  6. Linux - Port 端口检测方式

    ss 和 netstat 区别 netstat是遍历/proc下面每个PID目录: ss直接读/proc/net下面的统计信息. 所以ss执行的时候消耗资源以及消耗的时间都比netstat少很多 ne ...

  7. ZYNQ. GPIO

    GPIO General Purpose I/O ,网上能找到很多关于znyq gpio 的文章. 分类:EMIO .MIO .AXI_GPIO 硬件系统 MIO和EMIO是在zynq核中配置的,MI ...

  8. [转]避免头文件重复包含以及#ifndef 与 #program once 的区别

    为了避免同一个文件被include多次,C/C++中有两种方式,一种是#ifndef方式,一种是#pragma once方式.在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一 ...

  9. Shell-遍历删除指定目录

    Code: find $LibPath/ -name .svn | xargs rm -rf

  10. dblinks

    一.Oracle数据库链Database links的作用 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本 ...