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框架之Spring(二)

    前一篇博客讲述了Spring的一些基础概念,下面我们来创建第一个Spring程序吧. 步骤如下: 1) 导包 2) 配置文件 附没有提示的情况 MyEclipse ->File and Edit ...

  2. Linux 学习记录 四(Bash 和 Shell scirpt).

    一.什么是 Shell? 狭义的shell指的是指令列方面的软件,包括基本的Linux操作窗口Bash等,广义的shell则包括 图形接口的软件,因为图形接口其实也可以操作各种驱动程序来呼叫核心进行工 ...

  3. Frobenius norm(Frobenius 范数)

    Frobenius 范数,简称F-范数,是一种矩阵范数,记为||·||F. 矩阵A的Frobenius范数定义为矩阵A各项元素的绝对值平方的总和,即 可用于 利用低秩矩阵来近似单一数据矩阵. 用数学表 ...

  4. 洛谷P3987 我永远喜欢珂朵莉~(set 树状数组)

    题意 题目链接 Sol 不会卡常,自愧不如.下面的代码只有66分.我实在懒得手写平衡树了.. 思路比较直观:拿个set维护每个数出现的位置,再写个线段树维护区间和 #include<bits/s ...

  5. Tensorflow高效读取数据

    关于Tensorflow读取数据,官网给出了三种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow ...

  6. 我的Java之旅 第六课 JAVA WEB 请求与响应

    一.有关URL编码    1.在URL的规范中定义了一些保留字符,如::  /  ?  & =  @  % 等,在URI中有它的作用.如果要在URI中包含这些字符,必须转码,即%字符后跟十六进 ...

  7. 常见聚类算法——K均值、凝聚层次聚类和DBSCAN比较

    聚类分析就仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组(簇).其目标是,组内的对象相互之间是相似的,而不同组中的对象是不同的.组内相似性越大,组间差别越大,聚类就越好. 先介绍下聚类的不 ...

  8. zookeeper.Net

    原文转至:http://www.cnblogs.com/shanyou/p/3221990.html 之前整理过一篇文章<zookeeper 分布式锁服务>,本文介绍的 Zookeeper ...

  9. linux设置自动更换壁纸

    #!/bin/bash let n=0 files=($HOME/wallpapers/*.jpg) count=${#files[@]} while [ 1 ] do let "n=n%$ ...

  10. 洗礼灵魂,修炼python(18)--温故加知新

    类型转换: 1.str(),repr(),format():将非字符串数据转换为字符串 str():对象序列化的结果,相当于print输出 repr():程序中某个对象精确值 format():利用特 ...