1139 First Contact(30 分)

Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly in the first place. Instead, he might ask another boy C, one of his close friends, to ask another girl D, who was a friend of both B and C, to send a message to B -- quite a long shot, isn't it? Girls would do analogously.

Here given a network of friendship relations, you are supposed to help a boy or a girl to list all their friends who can possibly help them making the first contact.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (1 < N ≤ 300) and M, being the total number of people and the number of friendship relations, respectively. Then M lines follow, each gives a pair of friends. Here a person is represented by a 4-digit ID. To tell their genders, we use a negative sign to represent girls.

After the relations, a positive integer K (≤ 100) is given, which is the number of queries. Then K lines of queries follow, each gives a pair of lovers, separated by a space. It is assumed that the first one is having a crush on the second one.

Output Specification:

For each query, first print in a line the number of different pairs of friends they can find to help them, then in each line print the IDs of a pair of friends.

If the lovers A and B are of opposite genders, you must first print the friend of A who is of the same gender of A, then the friend of B, who is of the same gender of B. If they are of the same gender, then both friends must be in the same gender as theirs. It is guaranteed that each person has only one gender.

The friends must be printed in non-decreasing order of the first IDs, and for the same first ones, in increasing order of the seconds ones.

Sample Input:

10 18
-2001 1001
-2002 -2001
1004 1001
-2004 -2001
-2003 1005
1005 -2001
1001 -2003
1002 1001
1002 -2004
-2004 1001
1003 -2002
-2003 1003
1004 -2002
-2001 -2003
1001 1003
1003 -2001
1002 -2001
-2002 -2003
5
1001 -2001
-2003 1001
1005 -2001
-2002 -2004
1111 -2003

Sample Output:

4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0

题目大意:首先给出N个人之间的M个关系,表示A和B之间又关系(其中负值表示的是女生),再给出K个查询,表示A要追求B,就算有直接关系,但是也不使用,使用第三方关系。

输入要求:如果AB是异性,那么先输出的就是和A同性的人,如果AB是同性,那么他们的朋友输出的都必须是同性。

//这里的关系也不是一一对应,好像也不能用map.这个很难去遍历啊,首先需要找到A的一个同性朋友,如果AB异性,那么再找到异性朋友,再找到和B的同性朋友。

如果AB是同性,那么就一直找同性就可以了。

代码来自:https://www.liuchuo.net/archives/4210

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
unordered_map<int, bool> arr;
struct node {
int a, b;
};
bool cmp(node x, node y) {
return x.a != y.a ? x.a < y.a : x.b < y.b;
}
int main() {
int n, m, k;
scanf("%d%d", &n, &m);
vector<int> v[];
for (int i = ; i < m; i++) {
string a, b;
cin >> a >> b;
if (a.length() == b.length()) {//同性之间使用邻接表来存储。
v[abs(stoi(a))].push_back(abs(stoi(b)));
v[abs(stoi(b))].push_back(abs(stoi(a)));
}
//居然还有这种形式的邻接矩阵
//使用前4个0,表示一方,后四个0,表示另一方。不会出现重复。
arr[abs(stoi(a)) * + abs(stoi(b))] = arr[abs(stoi(b)) * + abs(stoi(a))] = true;
}
scanf("%d", &k);
for (int i = ; i < k; i++) {
int c, d;
cin >> c >> d;
vector<node> ans;
for (int j = ; j < v[abs(c)].size(); j++) {
for (int k = ; k < v[abs(d)].size(); k++) {
if (v[abs(c)][j] == abs(d) || abs(c) == v[abs(d)][k]) continue;
//如果下一个正好是对方,那么就跳过。
if (arr[v[abs(c)][j] * + v[abs(d)][k]] == true)
//如果在它们俩的同性之间有交集。
ans.push_back(node{v[abs(c)][j], v[abs(d)][k]});
//以这两个为参数新建一个结构体插入。
}
}
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", int(ans.size()));//需要强制转换为int并以%d输出。
for(int j = ; j < ans.size(); j++)
printf("%04d %04d\n", ans[j].a, ans[j].b);
}
return ;
}

//真是很厉害了。

1.使用unordered_map<int, bool> arr替代二维数组可避免内存超限。

2.这个arr的利用方法很特别。

3.将所有关系都存进这个arr形成一个邻接矩阵;

4.将所有同性关系存进去邻接表里

5.找到A认识的同性C,B认识的同性D,判断一下二者是否相等就可以了。

6.id会有0000和-0000,此时无法区分男女,所以需要用字符串输入。

7.使用stoi函数将字符串转化为int.

代码来自:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std; #define long long ll
const int MAXN = 1e4 + ;
const int INF = 0x7fffffff;
const int dx[]={, , , -};
const int dy[]={, , -, };
int n, m;
struct NODE{
vector<int>same;
}node[MAXN]; map<int, map<int, bool> >mp;//居然还可以这样来做邻接矩阵,厉害了。
struct ANS{
int idx, idy;
}ans[MAXN];
bool cmp(ANS x, ANS y){
if(x.idx==y.idx) return x.idy<y.idy;
return x.idx<y.idx;
} int main(){
while(~scanf("%d%d", &n, &m)){
mp.clear();
for(int i=; i<m; i++){
char x[], y[];
scanf("%s%s", x, y);
int s=abs(atoi(x)), t=abs(atoi(y));
mp[s][t]=mp[t][s]=true;//表示两者有联系方式
if(strlen(x)==strlen(y)){///同性
node[s].same.push_back(t);
node[t].same.push_back(s);
} }
int k;
scanf("%d", &k);
while(k--){
int s, t, cnt=;
scanf("%d%d", &s, &t);
s=abs(s), t=abs(t);
for(int i=; i<node[s].same.size(); i++){///s认识的同性x
for(int j=; j<node[t].same.size(); j++){///t认识的同性y
int x=node[s].same[i], y=node[t].same[j];
if(x==t || s==y) continue;//如果两者有直接关系,那么跳过。
if(mp[x][y]) ans[cnt].idx=x, ans[cnt++].idy=y;///判断x和y是否认识
}
}
sort(ans, ans+cnt, cmp);
printf("%d\n", cnt);
for(int i=; i<cnt; i++) printf("%04d %04d\n", ans[i].idx, ans[i].idy);
}
}
return ;
}

//这个构建邻接矩阵也十分不错,不会出现超市的 情况。厉害。

PAT 1139 First Contact[难][模拟]的更多相关文章

  1. PAT 1139 First Contact

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  2. PAT甲级1139 First Contact

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

  3. 1139 First Contact PAT (Advanced Level)

    原题链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 测试点分析: 首先来分析一下测试 ...

  4. pat advanced 1139. First Contact (30)

    题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...

  5. PAT 1085 Perfect Sequence[难]

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  6. PAT团体程序设计天梯赛 - 模拟赛

    由于本人愚笨,最后一题实在无力AC,于是只有前14题的题解Orz 总的来说,这次模拟赛的题目不算难,前14题基本上一眼就有思路,但是某些题写起来确实不太容易,编码复杂度有点高~ L1-1 N个数求和 ...

  7. PAT A1139 First Contact (30 分)——set

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  8. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  9. PAT 1042 Shuffling Machine[难]

    1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...

随机推荐

  1. javascript与 ios通讯解决办法

    阔别1年半之久,一个JavaScript和ios通讯的想法终于被实现了(我不知道别人有没有早就实现过~). 记得早期ios内嵌html做通讯时,貌似做好的办法只能是 ios通过url来截取页面发送消息 ...

  2. github前端资源

    摘要: 本文将分享我在github上常用的一些插件,可能在开发中你会用到它,希望能够帮助你! 前端技术总结 url  : https://github.com/JacksonTian/fks 简述: ...

  3. RabbitMQ消息确认(发送确认,接收确认)

    前面几篇记录了收发消息的demo,今天记录下关于 消息确认方面的 问题. 下面是几个问题: 1.为什么要进行消息确认? 2.rabbitmq消息确认 机制是什么样的? 3.发送方如何确认消息发送成功? ...

  4. 九度 1500:出操队形(LIS变形)

    题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较高的就要排在队尾.突然,有一天出操负责人想了一个 ...

  5. Effective C++ Item 19 Treat class design as type design

    Too high class topic for me now ................... ................... ................... fill the ...

  6. laravel windows下安装 gulp 和 laravel-elixir

    1)首先,确定一下你装了nodejs和npm了没?没装的话,到官网去下载最新版,传送门:https://nodejs.org/en/ npm 不需要单独安装,安装完 nodejs 就自带 npm 的了 ...

  7. osgEarth2.8添加模型

    #include <osgEarthDrivers/model_simple/SimpleModelOptions> SimpleModelOptions modelOptions; mo ...

  8. 说说新唐ARM9(未完待续)

    针对通用32位微控制器的NUC970系列嵌入了由RISC机器有限公司设计的RISC处理器ARM926EJ-S,运行频率高达300 MHz,具有16 KB的I-cache,16 KB的D-cache和M ...

  9. C++模板中的嵌套

    在下面的程序中,我们创建了一个模板类用于实现Queue容器的部分功能,并且在模板类中潜逃使用了一个Node类.queuetp.h // queuetp.h -- queue template with ...

  10. 【Spring Boot && Spring Cloud系列】构建Springboot项目 实现restful风格接口

    项目代码如下: package hello; import org.springframework.boot.SpringApplication; import org.springframework ...