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
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
vector<string> fri, sec;
map<string, map<string, int>>G;
int N, M;
typedef struct{
string a, b;
}node;
node list[];
bool cmp(node &nd1, node &nd2){
if(nd1.a != nd2.a){
return nd1.a < nd2.a;
}else{
return nd1.b < nd2.b;
}
}
int main(){
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
string v1, v2;
cin >> v1 >> v2;
G[v1][v2] = ;
G[v2][v1] = ;
}
int K;
scanf("%d", &K);
for(int i = ; i < K; i++){
string v1, v2;
cin >> v1 >> v2;
fri.clear();
sec.clear();
map<string, int>::iterator it;
for(it = G[v1].begin(); it != G[v1].end(); it++){
if(it->first != v1 && it->first != v2 && (it->first[] != '-' && v1[] != '-' || it->first[] == '-' && v1[] == '-'))
fri.push_back(it->first);
}
for(it = G[v2].begin(); it != G[v2].end(); it++){
if(it->first != v1 && it->first != v2 &&(it->first[] != '-' && v2[] != '-' || it->first[] == '-' && v2[] == '-'))
sec.push_back(it->first);
}
int pt = ;
for(int j = ; j < fri.size(); j++){
for(int k = ; k < sec.size(); k++){
if(G[fri[j]].count(sec[k]) != ){
string s1 = fri[j], s2 = sec[k];
if(s1[] == '-'){
s1.erase(, );
}
if(s2[] == '-'){
s2.erase(, );
}
list[pt].a = s1;
list[pt++].b = s2;
}
}
}
sort(list, list + pt, cmp);
printf("%d\n", pt);
for(int k = ; k < pt; k++){
cout << list[k].a <<" " <<list[k].b << endl;
}
}
cin >> N;
return ;
}

总结:

1、题意:A男和D女要想建立联系,需要中间人B和C,联系方式为A->B->C->D,其中B与A为同性,C与D为同性。给出一组A、D,求所有的B、C。

2、首先将整个图G存下来。先找B集合,B集合为与A有联系的同性;再找C集合,C集合为与D有联系的同性。 然后根据图G,找出B与C集合中相互有联系的点,即为所求。

3、关于如何存储图G。1)若使用临接矩阵则需要建立id到数组下标的映射,由于本题使用下标很多,这样做比较麻烦。2)还可以采用邻接表,使用二维的map,如map< map<int,int>>,mp[id1][id2] = 1表示id1与id2有联系, 或者使用map<int, int> mp[N]。 3)在别人的博客里看到了更好的办法,由于本题id都是4位纯数字,则若id1与id2有联系,将id1*10000 + id2 与id2*10000 + id1存在map<int, 随便>中,即可表示出id1与id2的关系。由此,若id是位数较少的字母+数字混合,也可以用这种办法

4、如何存储每个人的id。使用数字存储的话,+0和-0没法区分,从而无法区别性别会出现错误。 使用string存储较好,但要注意最后一个测试点可能超时。

5、在2中寻找B集合时,B有可能会包含A要联系的妹子D,这是不允许的,需要把D排除掉。寻找C集合同理。

A1139. First Contact的更多相关文章

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

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

  2. PAT甲级——A1139 First Contact【30】

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

  3. PAT_A1139#First Contact

    Source: PAT A1139 First Contact (30 分) Description: Unlike in nowadays, the way that boys and girls ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. 2014 Visual Studio Contact(); 直播笔记

    昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS)和Visual Studio免费(Visual Studio ...

  6. GConf error:Failed to contact configuration server

    Linux系统运行一直正常,但是图形界面使用root账号登录时遇到下面错误,第一次遇到这么怪异的状况 具体错误信息如下所示: GConf error:Failed to contact configu ...

  7. 【USACO 3.1】Contact(01子串按出现次数排序)

    题意:给你一个01字符串,将长度为a到b之间(包含a.b)的子串按照出现次数排序.注意输入输出格式 题解:01子串对应一个二进制,为了区别11和011这样的不同子串,我们把长度也记录下来,官方题解是在 ...

  8. Contact项目梳理

    1. 共三张表:user用户表  group分组表 contact联系人表 entity  分模块,三个实体类,三个模块 2. 先注册再登录 DAO:UserDAOImpl public User g ...

  9. 01 选择 Help > Install New Software,在出现的对话框里,点击Add按钮,在对话框的name一栏输入“ADT”,点击Archive...选择离线的ADT文件,contact all update ....千万不要勾选点击Add按钮,在对话框的name一栏输入“ADT”,点击Archive...选择离线的ADT文件,contact all update ....千万不要勾

    引言 好久没碰过android,今天重新搭建了一次环境,遇到的问题记录下载.共以后使用. 安装 软件的软件有jdk+eclipse+adt+sdk 主要记录安装adt和sdk的过程,注意,adt和sd ...

随机推荐

  1. Day 5-4封装.__隐藏属性或者方法

    封装 property 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏. 在python中用双下划线开头的方式将属性隐藏起来(设置成 ...

  2. MySqlHelper的封装

    其实MySqlHelper和SqlHelper是一样的,只是使用的驱动不一样而已. public class MySQLHelper { public static final String url ...

  3. HTML5经典实例——1基础语法和语义

    1指定DOCTYPE 在页面的最开始处指定HTML5 DOCTYPE DOCTYPE是不区分大小写的.可以任意的使用大小写. <!DOCTYPE html> <html lang=& ...

  4. maven+springmvc项目启动时,request mapping not found……

    springmvc项目跑的好好的,跑着跑着,出现request mapping not found的问题. 第一波,网上查问题,stackoverflow上面的各种配置说明,但是我本地就是没查出问题 ...

  5. 莫烦scikit-learn学习自修第四天【内置训练数据集】

    1. 代码实战 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from sklearn import datasets from sklearn.linea ...

  6. Apache的commons工具类

    package cn.zhou; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileU ...

  7. nodejs zip 安装配置

    1.下载 下载地址:https://nodejs.org/zh-cn/download/ 选择相应的版本下载 2.解压缩 将文件解压到要安装的位置,并新建两个目录 node-global :npm全局 ...

  8. Opencv画图操作

    1. 画矩形 MyRect rect;rect.left = 5;rect.top = 5;rect.right = 100;rect.bottom = 100;IplImage * pColorIm ...

  9. Nginx 滑动窗口与缓冲区

    L:125

  10. 法语Linux NuTyX 11 RC2 发布

    读 NuTyX是一个法语Linux发行版(具有多语言支持),由Linux From Scratch和Beyond Linux From Scratch构建,带有一个名为“cards”的自定义包管理器. ...