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. 【读书笔记】iOS-发布你的应用

    一,添加图标 你的应用在iPhone主屏幕上的标准图标(Icon.png)是57像素*57像素的正方形,PNG格式,不能有透明效果或者图层,72DPI.除些之外,你还可以提供一个同样格式的114像素* ...

  2. logo.ico在html网页中的实现

    <!doctype html><html> <head> <meta charset="utf-8"/> <title> ...

  3. Installing Language Tool in TexStudio

    This is a recent and more detailed solution for Windows users. Make sure the last version of TeXstud ...

  4. Linux 查看本机串口方法

    最近在了解嵌入式方面的知识,就随笔记录一下: 查看Linux本机串口: 1.查看串口是否可用 可以对串口发送数据比如对com1口,echo /dev/ttyS02.查看串口名称使用 ls -l /de ...

  5. 对haproxy文件进行增删改查

    1.文件内容 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global ...

  6. 【PAT】B1051 复数乘法(15 分)

    要会使用math函数, 还要注意到用四舍五入的方法判断是否应该输出0.00 #include <math.h> #include<stdio.h> int main() { d ...

  7. 10LaTeX学习系列之---Latex的文档结构

    目录 目录 前言 (一)对于Ctex宏包中的文档结构 1.说明 2.源代码 3.输出效果 4.技巧 (二)对于ctexart的文档结构 1.说明 2.源代码 3.输出效果 (三)对于ctexbook的 ...

  8. ccf--20150903--模板生成系统

    本题思路:首先,使用一个map来存储所有需要替换的关键词,然后,再逐行的替换掉其中的关键词,记住,find每次的其实位置不一样,否则会出现递归生成没有出现关键词就清空掉.最后输出. 题目和代码如下: ...

  9. 接上篇:将OneDrive云盘挂载到我的电脑!(1024快乐,明年我应该也可以过这个节日了!)

    今天对程序猿来说是个值得纪念的日子!祝程序员小哥哥小姐姐们今天可以早早下班,回家休息,Bug走开! 接上篇,将自己申请的5T云盘挂载到我的电脑! 第一步:挂网下载Raidrive 附上链接: http ...

  10. Android Studio 学习Demo内容及一些bug处理技巧 -----个人技术文档,两次冲刺总结

    实现的基本内容 1.基本界面的注册(包括转换界面,隐式,显式注册,主界面的入口注册) 2.匿名内部类实现Button按钮的监听事件,并通过Toast进行显示 3.界面切换(显式.隐式) 4.调用浏览器 ...