PAT 1139 First Contact[难][模拟]
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[难][模拟]的更多相关文章
- PAT 1139 First Contact
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- PAT甲级1139 First Contact
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...
- 1139 First Contact PAT (Advanced Level)
原题链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 测试点分析: 首先来分析一下测试 ...
- pat advanced 1139. First Contact (30)
题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- PAT团体程序设计天梯赛 - 模拟赛
由于本人愚笨,最后一题实在无力AC,于是只有前14题的题解Orz 总的来说,这次模拟赛的题目不算难,前14题基本上一眼就有思路,但是某些题写起来确实不太容易,编码复杂度有点高~ L1-1 N个数求和 ...
- PAT A1139 First Contact (30 分)——set
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- PAT 1042 Shuffling Machine[难]
1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...
随机推荐
- PHP实现一个ip(如:127.0.0.1)和多个域名(虚拟主机)的绑定
解决方案一:通过端口来区分不同的虚拟主机 ①按照绑定一个站点的方法做好准备 1. 先开发好自己的网站(d:/myblog(存放在D盘的myblog目录下)) 2. 配置httpd.conf文件(存放在 ...
- 理解ASP.NET MVC中的ActionResult
通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...
- List 和 ObservableCollection的区别
在WPF中绑定一个集合的时候,比如:DataGrid.ItemsSource = new List<T>(); 这样的操作,会存在当数据行新增或者删除的时候不会得到及时的通知来刷新界面,而 ...
- Android 6.0启动过程具体解析
在之前的一篇文章中.从概念上学习了Andoird系统的启动过程.Android系统启动过程学习 而在这篇文章中,我们将从代码角度细致学习Android系统的启动过程,同一时候,学习Android启动过 ...
- Java精选笔记_IO流(字节流、InputStream、OutputStream、字节文件、缓冲区输入输出流)
字节流 操作图片数据就要用到字节流. 字符流使用的是字符数组char[],字节流使用的是字节数组byte[]. 字节流读写文件 针对文件的读写,JDK专门提供了两个类,分别是FileInputStre ...
- 安装autoit libary失败问题解决
运行环境:win7-32,python2.7安装遇到的问题:
- RF使用的相关库API
RF内置库: http://robotframework.org/robotframework/ SSHLibrary: ---WEB自动化测试 http://robotframework.org ...
- Python 内部类
内部类也就是在类的内部再定义类,如下: #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): class Chinese( ...
- coreseek/sphinx CentOS6.4下安装
一.在CentOS6.4下安装coreseek之前需要预先安装以下软件 1.打开终端 输入 su 获取管理员权限 2.输入命令 yum install make gcc g++ gcc-c++ lib ...
- web基础----->模板引擎Velocity的使用(一)
Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言可以使用在 Java 中定义的对象和变量上.今天我们就学习一下Velocity的用法. Velocity的第一个例子 项目的主体是 ...