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. ...
随机推荐
- Windows下 wamp下Apache配置虚拟域名
安装好wamp后 找到 找到 Include conf/extra/httpd-vhosts.conf 去掉前面的# 并保存 修改 DocumentRoot 和 ServerName ...
- 巧妙的使用jmeter来发送json格式数据
1. header-manager 修改content-type值. 如果不修改该值, 则默认会是urlencode的数据格式(例如a=5&b=6). 修改为json后,会告诉服务器,发送的数 ...
- Linux(四)—— 项目部署与ansible自动化部署
目录 项目部署与ansible自动化部署 一.项目部署 二.ansible自动化部署(python自动化运维) 1.安装ansible 2.ansible例子 3.ansible自动化部署nginx ...
- 05、python的基础-->字典的增、删、改、查
1.字典的增 dict = {'age':19,'name':'老王','hobby':'girl'} dict['sex'] = 'boy' #没有键值对,直接添加 dict[' #有键值对,覆盖值 ...
- 转 关于Raid0,Raid1,Raid5,Raid10的总结
关于Raid0,Raid1,Raid5,Raid10的总结 RAID0 定义: RAID 0又称为Stripe或Striping,它代表了所有RAID级别中最高的存储性能.RAID 0提高存储性能 ...
- Http,Socket,TCP/IP 协议简述
Http,Socket,TCP/IP 协议简述:https://blog.csdn.net/gordohu/article/details/54097841 TCP/IP协议,HTTP协议与webSo ...
- Python37不能启动pyspider
报错内容: Traceback (most recent call last): File "/usr/local/var/pyenv/versions/3.7.3/bin/pyspider ...
- Vue 单页应用 的 首屏优化
对于单页应用,要在一个页面上为用户提供产品的所有功能,在这个页面加载的时候,首先要加载大量的静态资源,这个加载时间相对比较长.所以我们需要做一些相应的优化,减少响应时间,尽快把首屏显示出来. 1.压缩 ...
- idea的spring配置
开始使用idea,开始的时候把相关的插件都禁用了,导致在建项目和configuration的时候不出现spring相关字样 到plugins中找到installed的插件,查看spring boot ...
- Ubuntu下串口工具
一.Kermit 1.安装: sudo apt-get install ckermit 2.配置: sudo gedit /etc/kermit/kermrc 3.在文件末端添加如下内容 : set ...