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. 【转自IT虾米网:www.itxm.net】外部应用和drools-wb6.1集成解决方案

    一.手把手教你集成外部应用和drools workbench6.1 1.         首先按照官方文档安装workbench ,我用的是最完整版的jbpm6-console的平台系统,里面既包含j ...

  2. mysqldump工具,工作的本质是什么呢?(dump表的时候,是否会产生drop表的语句)

    需求描述: 今天在看mysqldump工具的使用过程,发现一个同事,是这样写的mysqldump命令 mysqldump -uroot -pmysql employees titles | mysql ...

  3. [redis] redis 对string类型数据操作

    package com.xwolf.java.redis; import org.junit.Before; import org.junit.Test; import redis.clients.j ...

  4. [java] java 中Unsafe类学习

    java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操作,主要提供了以下功能: 1.通过Unsafe类可以分配内存,可以释放内存: 类中提供的3个本地方法all ...

  5. swift - 之 UIColor使用自定义的RGB配色

    1.10进制颜色 UIColor(red: /, green: /, blue: /, alpha: 0.5) 2.16进制颜色 UIColor(red: , green: , blue: , alp ...

  6. 解决Qt程序在Linux下无法输入中文的办法(与下文连接)

    在安装QT集成开发工具包之前需要先安装build-essential和libncurses5-dev这两个开发工具和库,libncurses5-dev库是一个在Linux/Unix下广泛应用的图形函数 ...

  7. iOS性能调优系列(全)

    总结: 三类工具 基础工具 (NSLog的方式记录运行时间.) 性能工具.检测各个部分的性能表现,找出性能瓶颈 内存工具.检查内存正确性和内存使用效率 性能工具: 可以衡量CPU的使用,时间的消耗,电 ...

  8. 关于Windows下的批处理如何模拟Sleep

    好好的批处理,居然没有正式的Sleep可供调用.有时候,确实感到很无趣. 1. 方法1: ping 1.1.1.1来模拟 好不容易从stackoverflow上找到一个答案(称之为答案,是因为它被人标 ...

  9. cache buffers chains以及热块解决方案

    cache buffers chains以及热块解决方案 今天是2013-10-10,今天下午我调休了,中午饭过后从14点一直睡到16点,这种感觉真爽.  之前学习过关于buffer cache的ca ...

  10. LeetCode——Convert Sorted Array to Binary Search Tree

    Description: Given an array where elements are sorted in ascending order, convert it to a height bal ...