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 <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <utility>
using namespace std;
const int maxn=;
int n,m,k;
set<int> same[maxn],dif[maxn];
int gender[maxn]={};
vector<int> path[maxn],tmppath;
bool vis[maxn];
int num=;
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<m;i++){
string p1,p2;
cin>>p1>>p2;
int aid,bid;
aid=abs(stoi(p1));
bid=abs(stoi(p2));
if(p1[]=='-'){
gender[aid]=-;
}
if(p2[]=='-'){
gender[bid]=-;
}
if(gender[aid]==gender[bid]){
same[aid].insert(bid);
same[bid].insert(aid);
}
else{
dif[aid].insert(bid);
dif[bid].insert(aid);
}
}
scanf("%d",&k);
int x=;
for(int i=;i<k;i++){
int p1,p2;
scanf("%d %d",&p1,&p2);
vector<int> ans;
fill(vis,vis+maxn,false);
vis[abs(p1)]=true;
vis[abs(p2)]=true;
if(gender[abs(p1)]==gender[abs(p2)]){
for(auto it=same[abs(p1)].begin();it!=same[abs(p1)].end();it++){
if(vis[*it]==false){
for(auto it2=same[abs(p2)].begin();it2!=same[abs(p2)].end();it2++){
if(vis[*it2]==false){
if(same[*it].find(*it2)!=same[*it].end()){
ans.push_back(*it);
ans.push_back(*it2);
}
}
}
}
}
}
else{
for(auto it=same[abs(p1)].begin();it!=same[abs(p1)].end();it++){
if(vis[*it]==false){
for(auto it2=same[abs(p2)].begin();it2!=same[abs(p2)].end();it2++){
if(vis[*it2]==false){
if(dif[*it].find(*it2)!=dif[*it].end()){
ans.push_back(*it);
ans.push_back(*it2);
}
}
}
}
}
}
printf("%d\n",ans.size()/);
for(int j=;j<ans.size();j+=){
printf("%04d %04d\n",ans[j],ans[j+]);
}
} }

注意点:这种有正负两种的要开一个数组保存结果,然后分情况的可以开多个数组或是set保存不同结果,不要都保存在一起。最后输出4位整数要注意一下。

PAT A1139 First Contact (30 分)——set的更多相关文章

  1. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  2. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

  3. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  4. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  5. PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  6. PAT 垃圾箱分布(30分)dijstra

    垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾 ...

  7. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  8. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  9. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

随机推荐

  1. 【Java基础】13、抽象方法不能是static或native或synchroniz 原因及误解

    在网上看到过这么一篇文章,是关于抽象方法不能是static或native或synchroniz 的原因.其中提到了这些关键字的意义以及与 abstract 关键字的冲突,大体内容如下: 1.abstr ...

  2. C#设计模式六大原则概述

    在面向对象的设计中,我们应当遵循以下六大原则,只有掌握了这些原则,才能更好的理解设计模式. 1:单一职责原则(Single Responsibility Principle 简称 :SRP) : 就一 ...

  3. CSS总结div中的内容垂直居中的五种方法

    一.行高(line-height)法 如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如: p { height:30px; line-height:3 ...

  4. LVS主从部署配置和使用

    LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. LVS是L ...

  5. 传统BI还是自助式BI---BI与数据分析 ZT

    自助式BI或者自助式数据分析是最近几年兴起的一个概念.根据Gartner发布的信息,Self Service Business Intelligence(SSBI)被定义为“终端用户在被批准和支持的平 ...

  6. loadrunner 运行场景-运行时设置

    运行场景-运行时设置 by:授客 QQ:1033553122 A.   查看.修改单个脚本的运行时设置 a)   途径1: Scenario Groups.Scenario Groups Script ...

  7. 天池新人赛-天池新人实战赛o2o优惠券使用预测(一)

    第一次参加天池新人赛,主要目的还是想考察下自己对机器学习上的成果,以及系统化的实现一下所学的东西.看看自己的掌握度如何,能否顺利的完成一个分析工作.为之后的学习奠定基础. 这次成绩并不好,只是把整个机 ...

  8. 使用sklearn机器学习库实现线性回归

    import numpy as np  # 导入科学技术框架import matplotlib.pyplot as plt  # 导入画图工具from sklearn.linear_model imp ...

  9. scrapy简单分布式爬虫

    经过一段时间的折腾,终于整明白scrapy分布式是怎么个搞法了,特记录一点心得. 虽然scrapy能做的事情很多,但是要做到大规模的分布式应用则捉襟见肘.有能人改变了scrapy的队列调度,将起始的网 ...

  10. word中从正文开始编码的方法

    假如第1页和第2页是首页和目录,你想从第3页的正文开始设置页码 1. 将光标移到第二页的最后位置,点击插入“分隔符”,选择“分节类型”的“下一页”,确定.这时光标自动移到第三页. 2. 点击插入页码, ...