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. ...
随机推荐
- 极致CMS建站系统后台GETSHELL
起因 正在学习代码审计 看到有人提交了一个注入https://www.cnvd.org.cn/flaw/show/CNVD-2019-42775 想试试看还有没有别的漏洞 受影响版本 v1.6.3 - ...
- PHP代码审计基础
php核心配置 php.ini 基本配置 语法 大小写敏感 运算符 空值的表达式 安全模式 安全模式 safe_mode = off 用来限制文档的存取,限制环境变量的存取,控制外部程序的执行.PHP ...
- Linux应急响应基础
文件排查 敏感目录文件分析 tmp目录 命令目录 /usr/bin /usr/sbin 开机启动项 /etc/init.d /etc/init.d是/etc/rc.d/init.d的软链接 文件时间 ...
- Java不可变对象
在创建状态后无法更改其状态的对象称为不可变对象.一个对象不可变的类称为不可变类.不变的对象可以由程序的不同区域共享而不用担心其状态改变. 不可变对象本质上是线程安全的. 示例 以下代码创建了不可变类的 ...
- 常用命令--mount
mount -o remount,rw / mount 命令 [-t 文件系统] [-L 卷标名] [-o 特殊选项] 设备文件名 挂载点 -l 查询系统中已经挂载的设备,-l 会显示卷标 -a ...
- netty的原理
1. Netty简介 Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作 ...
- CF1220F
CF1220F 把整棵树分成1的左边和1的右边两部分 最优情况两边子树深度的差一定可以是一 如果还可以是2,也可以通过把多的那一边的点往另一边移使他变成1 如果往一个端点加点,一定不会使这一边变优,也 ...
- Oracle如何杀同库不同实例的会话
今天处理了一个生产上的问题,主要就是杀会话, 生产环境是Oracle11gR2 RAC:有同事开发报表,报表工具连接到数据库上特别嚣张,把内存pin住: Select s.INST_ID, s.Mac ...
- 项目案例之GitLab的数据迁移
项目案例之GitLab的数据迁移 链接:https://pan.baidu.com/s/1CgaEv12cwfbs5RxcNpxdAg 提取码:fytm 复制这段内容后打开百度网盘手机App,操作更方 ...
- git的一些简单用法
1.工作区暂存区和仓库区 工作区 对于添加.修改.删除文件的操作,都发生在工作区中 暂存区 暂存区指将工作区中的操作完成小阶段的存储,是版本库的一部分 仓库区 仓库区表示个人开发的一个小阶段的完成 仓 ...