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. windows 2008 r2 安装TabsStudio

    windows 2008 r2 安装TabsStudio 办法如下: HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer.如果没有这个项,则新建这个项 ...

  2. git for c#, commit本地,pushserver

    //ok private static void push() { string wkDir = @"E:\DotNet2010\单位project\Git.Client\lib2Test\ ...

  3. shell基础篇(九)函数

    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高.像其他编程语言一样,Shell 也支持函数.Shell 函数必须先定义后使用1. Shell 函数的定义格式如下: ...

  4. 元素设置disabled属性后便无法向后台传值

    元素设置disabled属性后便无法向后台传值

  5. 【ecshop】使用sql 清除测试数据

    操作方式:后台->数据库->sql查询   输入以下你想进行的操作 -- -- 清空会员有关数据: -- TRUNCATE TABLE `ecs_users` ; TRUNCATE TAB ...

  6. MQTT协议笔记之mqtt.io项目HTTP协议支持

    前言 MQTT协议诞生之初,就未曾考虑通过HTTP传输.这也正常,网络受限.不稳定网络不太适合HTTP(2G/3G网络大家使用WAP不也OK嘛).在网络较为充裕的桌面端而言,虽纯文本对比二进制而言没多 ...

  7. Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签 《转载》

    摘录某个工程的 web.xml 文件片段: 访问顺序为1—>2—>3—>4,其中2和3的值必须相同. url-pattern 标签中的值是要在浏览器地址栏中输入的 url,可以自己命 ...

  8. 【BZOJ1045】[HAOI2008] 糖果传递 贪心

    [BZOJ1045][HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正 ...

  9. linux下安装F-prot杀毒软件

    一. f-prot的安装 1.首先我们要创建一个带有超级权限的用户 sudo passwa root 2.su 切换用户 3.下载F-prot http://www.f-prot.com/downlo ...

  10. c# winform窗体边框风格的设计

    1.首先,窗体的FormBorderStyle设置成None,不要控制边框. 2.然后,TransparencyKey和BackColor颜色设置成相同的,这样,窗体就透明了. 3.最后,窗体的拖动 ...