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
当初考试的时候只得了一部分的分,现在拿出来重新做,思路清晰了很多,对于A,找一个同性好友C,C再找个好友D,D要是B的同性好友,男女编号绝对值可能一样,所以女编号用加10000来区分,但是0000和-0000,通过int读取是无法区分性别的,所以选择字符串读取。
用vector存某人的好友,也方便查找给定一个编号判断是否有这个好友。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
vector<int> s[];///存好友 下标代表本人
P ans[];///存答案
int vis[];///标记是否排序过
int n,m,k,num;
char a[],b[];
inline int change(char* t) {///字符串转换为int
int d = ,i = ;
if(t[] == '-')d = ,i ++;///负数直接绝对值加10000来区分
while(t[i]) {
d = d * + t[i ++] - '';
}
return d;
}
inline bool check(int x,int y) {///检查是否同性 以10000为界限
if(x >= && y >= || x < && y < )return true;
return false;
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < m;i ++) {
scanf("%s%s",a,b);
int aa = change(a);
int bb = change(b);
s[aa].push_back(bb);
s[bb].push_back(aa);
}
scanf("%d",&k);
while(k --) {
scanf("%s%s",a,b);
int aa = change(a);
int bb = change(b);
if(!vis[aa]) {
vis[aa] = ;
sort(s[aa].begin(),s[aa].end());
}
num = ;
for(int i = ;i < s[aa].size();i ++) {
if(s[aa][i] != bb && check(aa,s[aa][i])) {///先找A的同性好友C 要保证不是B AB可能同性
int t = s[aa][i];
if(!vis[t]) {
vis[t] = ;
sort(s[t].begin(),s[t].end());
}
for(int j = ;j < s[t].size();j ++) {
///找C的好友D 保证D不等于A 如果D和B同性 且是B好友则满足
if(s[t][j] != aa && check(bb,s[t][j]) && find(s[s[t][j]].begin(),s[s[t][j]].end(),bb) != s[s[t][j]].end()) {
ans[num ++] = P(t,s[t][j]);
}
}
}
}
printf("%d\n",num);
for(int i = ;i < num;i ++) {
printf("%04d %04d\n",ans[i].first % ,ans[i].second % );
}
}
}

1139 First Contact(30 分)的更多相关文章

  1. pat advanced 1139. First Contact (30)

    题目链接 解法暴力 因为有 0000, -0000 这样的数据,所以用字符串处理 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue #include<cstdio> ...

  2. PAT 1139 First Contact[难][模拟]

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  3. PTA 07-图5 Saving James Bond - Hard Version (30分)

    07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie ...

  4. PTA 社交网络图中结点的“重要性”计算(30 分)

    7-12 社交网络图中结点的“重要性”计算(30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互 ...

  5. L3-015 球队“食物链” (30 分)

    L3-015 球队“食物链” (30 分)   某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席 ...

  6. 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 ...

  7. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  8. PTA 7-2 二叉搜索树的结构(30 分)

    7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...

  9. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  10. 【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. 解决mysql数据库乱码问题

    MySQL的SQL语言是用于访问数据库的最常用标准化语言.MySQL软件采用了双授权政策,它分为社区版和商业版,由于其体积小.速 度快.总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选 ...

  2. Makefile调用shell应该注意的地方

    转载:http://blog.csdn.net/ninlei0115/article/details/9732191 1.在Makefile中只能在target中调用Shell脚本,其他地方是不能输出 ...

  3. Web网页开发常见经典问题

    1.网络请求参数共享 转发dispatcher和重定向redirect 对于参数共享的区别 Redirect和Dispatcher 区别

  4. ASP.NET动态网站制作(10)-- JQ(2)

    前言:jq的第二节课. 内容: 1.管理选择结果:  (1)获取元素个数:$("img").size():获取页面中所有“img”个数:  (2)提取元素:$("img[ ...

  5. Android-理解window和windowmanager

    1.window和windowmanager的关系 window是一个抽象类,具体实现为phoneWindow.创建一个window可以通过windowmanager来实现. window的具体实现在 ...

  6. C# WinForm退出方法

    1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit();  强制所有消息中 ...

  7. rtmp直播拉流客户端EasyRTMPClient TCP窗口大小设计方法

    EasyRTMPClient 简介 EasyRTMPClient是EasyDarwin流媒体团队开发.提供的一套非常稳定.易用.支持重连接的RTMPClient工具,以SDK形式提供,接口调用非常简单 ...

  8. EasyDarwin在做拉模式转发海康RTSP摄像机视频流的过程中出现花屏问题的解决方案

    问题描述 在3年前我当时基于EasyDarwin为用户开发了一款RTSP拉模式转发的程序,也发布了一篇博客<用Darwin开发RTSP级联服务器(拉模式转发)>,当时考虑的很简单,只要将R ...

  9. ubuntu 安装codeblocks

    本文转载于:http://blog.csdn.net/i_fuqiang/article/details/9749225 1.安装gcc: sudo apt-get install build-ess ...

  10. 九度OJ 1023:EXCEL排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:14605 解决:3307 题目描述:     Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能.     对每个测试用例 ...