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. ...
随机推荐
- flex 布局 实现三点筛子
实现麻将中三点筛子:效果如下图 具体实现代码: html代码: <div class="box"> <div class="item"> ...
- margin 负值问题
* margin-top 和 margin-left 负值,自身元素向上.向左移动: * margin-right 负值,右侧元素左移,自身元素不受影响: * margin-bottom 负值,下方元 ...
- Hive 时间操作函数(转)
1.日期函数UNIX时间戳转日期函数: from_unixtime 语法: from_unixtime(bigint unixtime[, string format]) 返回值: string ...
- 二级域名解析设置及Apache 子域名配置
域名管理解析项 如: cy.wanggangg.top 为wanggangg.top域名添加解析 主机记录设为 cy 记录值 为服务器ip地址 打开apache配置文件 新增如下:<Virtua ...
- WinDows应急响应基础
文件排查 开机启动有无异常文件 msconfig 敏感的文件路径 %WINDIR% %WINDIR%\SYSTEM32\ %TEMP% %LOCALAPPDATA% %APPDATA% 用户目录 新建 ...
- python学习笔记:json与字典的转换(dump 和dumps load和loads的区别)
1. json序列化(字典转成字符串)方法: dumps:无文件操作 dump:序列化+写入文件 2. json反序列化(字符串转成字典)方法: loads:无文件操作 ...
- python_ 模块 json pickle shelve
一,什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码( ...
- zabbix--ODBC 监控mysql
zabbix ODBC 数据库监控ODBC 是 C 语言开发的.用于访问数据库的中间件接口.zabbix 支持查询任何 ODBC 支持的数据库.zabbix 通过调用ODBC 来获取数据库的数据以及数 ...
- ArcGis 创建含孔洞面要素AO C#
IGeometryCollection geometryCollection = new PolygonClass(); IPointCollection pointCollection_Exteri ...
- fragment中的onCreateView和onViewCreated的区别和
(1) onViewCreated在onCreateView执行完后立即执行. (2) onCreateView返回的就是fragment要显示的view.