PAT_A1139#First Contact
Source:
Description:
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
Keys:
- 哈希映射
Attention:
- 首先各结点的id是不同的,在这个基础上,增加负号区分男女生,一开始想多了0,0
- 注意+0和-0
- 注意f1!=p2 && f2!=p1
Code:
/*
Data: 2019-08-07 20:48:39
Problem: PAT_A1139#First Contact
AC: 36:48 题目大意:
早年的蓝孩子和吕孩子都是很羞涩的0,0
联系方式:B1 -> B2 -> G2 -> G1
给一张关系网,能否帮助B1联系到G1
输入:
第一行给出,人数N<=300,关系数M
接下来M行,A与B认识,女生添加负号
接下来一行,查询次数K<=100
接下来K行,A和B,帮A给B送秋波
输出:
第一行给出,多少对朋友可以牵线搭桥
接下来N行,字典序递增 基本思路:
frd存储同性之间的关系网,
net判断任意两人之间是否为朋友,
依次遍历A的朋友和B的朋友,
如果A的朋友和B的朋友有关系,则存储这队朋友;
排序,打印;
*/
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=1e4;
struct node
{
int f1,f2;
};
map<int,int> mp,net;
vector<int> frd[M]; bool cmp(const node &a, const node &b)
{
if(a.f1 != b.f1)
return a.f1 < b.f1;
else
return a.f2 < b.f2;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,m,p1,p2;
string v1,v2;
scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
cin >> v1 >> v2;
p1=abs(atoi(v1.c_str()));
p2=abs(atoi(v2.c_str()));
mp[p1]=mp[p2]=;
net[p1*M+p2]=net[p2*M+p1]=;
if(v1.size()==v2.size())
{
frd[p1].push_back(p2);
frd[p2].push_back(p1);
}
}
scanf("%d", &m);
while(m--)
{
scanf("%d%d", &p1,&p2);
p1=abs(p1);
p2=abs(p2);
if(mp[p1]== || mp[p2]==)
{
printf("0\n");
continue;
}
vector<node> ans;
for(int i=; i<frd[p1].size(); i++)
{
for(int j=; j<frd[p2].size(); j++)
{
int f1=frd[p1][i],f2=frd[p2][j];
if(p1==f2 || p2==f1) continue;
if(net[f1*M+f2])
ans.push_back(node{f1,f2});
}
}
sort(ans.begin(),ans.end(),cmp);
printf("%d\n", ans.size());
for(int i=; i<ans.size(); i++)
printf("%04d %04d\n", ans[i].f1,ans[i].f2);
} return ;
}
PAT_A1139#First Contact的更多相关文章
- 2014 Visual Studio Contact(); 直播笔记
昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS)和Visual Studio免费(Visual Studio ...
- GConf error:Failed to contact configuration server
Linux系统运行一直正常,但是图形界面使用root账号登录时遇到下面错误,第一次遇到这么怪异的状况 具体错误信息如下所示: GConf error:Failed to contact configu ...
- 【USACO 3.1】Contact(01子串按出现次数排序)
题意:给你一个01字符串,将长度为a到b之间(包含a.b)的子串按照出现次数排序.注意输入输出格式 题解:01子串对应一个二进制,为了区别11和011这样的不同子串,我们把长度也记录下来,官方题解是在 ...
- Contact项目梳理
1. 共三张表:user用户表 group分组表 contact联系人表 entity 分模块,三个实体类,三个模块 2. 先注册再登录 DAO:UserDAOImpl public User g ...
- 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 ...
- Spring-JDBC实现Contact的CRUD
Spring-JDBC完成Contact的CRUD. 两点注意: 1.log4j.properties文件不能少 2.注意导入的包之间的依赖关系以及版本要求. 项目结构: 主要文件: 建表脚本: CR ...
- Account problem-There may be a problem with your account. Please contact us. Sign out
很多人在使用开发者账号AppleID的时候,都会碰到如下问题 There may be a problem with your account. Please contact us. 登录到苹果的开发 ...
- 解决方法:An error occurred on the server when processing the URL. Please contact the system administrator
在WINDOWS7或SERVER2008上安装了IIS7.5,调试ASP程序时出现以下错误: An error occurred on the server when processing the U ...
- [转]jQuery Popup Login and Contact Form
本文转自:http://www.formget.com/jquery-popup-form/ Pop up forms are the smart way to present your site. ...
随机推荐
- HTML5: HTML(5) 代码规范
ylbtech-HTML5: HTML(5) 代码规范 1.返回顶部 1. HTML(5) 代码规范 HTML 代码约定 很多 Web 开发人员对 HTML 的代码规范知之甚少. 在2000年至201 ...
- spring boot找不到或无法加载主类 io.renren.RenrenApplication
spring boot找不到或无法加载主类 io.renren.RenrenApplication 出现问题: spring boot 项目以前一直是好好的,用mvn clean package 打包 ...
- 大数据学习笔记之Hadoop(一):Hadoop入门
文章目录 大数据概论 一.大数据概念 二.大数据的特点 三.大数据能干啥? 四.大数据发展前景 五.企业数据部的业务流程分析 六.企业数据部的一般组织结构 Hadoop(入门) 一 从Hadoop框架 ...
- webstorm 分屏
- 弹出框中的AJAX分页
$(function() { $("body").on("click",".set-topic",function(){ /*获取所有题目接 ...
- 火狐foxyproxy + burp
下载 火狐foxyproxy 和 burp 两个代理ip端口填写一致 如果对于公司有正向代理服务器,则需要配置burp的上游代理 对于IE浏览器的代理是windows操作系统的全局代理,在ie配置代理 ...
- UVA1152_4 Values whose Sum is 0
中途相遇法,这题目总结后我感觉和第一篇博客很像,他们都取了中间,也许这就是二分的魅力吧 这题题意就是从ABCD四个集合中选四个元素使他们的和为0 题意很简单,但是实现起来很容易超时,不能一个一个枚举 ...
- 在响应式布局中,碰到图片不会自动缩放,因此需要一段js脚本
<script> (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchang ...
- 开发效率优化之Git分布式版本控制系统(一)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680本篇文章将先从Git分布式版本控制系统来阐述开发效率优化 一,企业 ...
- git拉取远程所有分支
第一步: git branch -r | grep -v '->' | while read remote; do git branch --track "${remote#origi ...