PAT A1139 First Contact (30 分)——set
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的更多相关文章
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT 垃圾箱分布(30分)dijstra
垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- 【PAT】1053 Path of Equal Weight(30 分)
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
随机推荐
- 【协议】3、HTTP 协议入门
HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它成为技术热点. 本文介绍 HTTP 协议的历史演变和设计思路. 一.HTTP/0.9 HTTP 是基于 TCP ...
- 使用Swagger2时遇到的问题
Swagger2使用起来很简单,加一个@EnableSwagger2注解,并引入如下依赖就ok了 <dependency> <groupId>io.springfox</ ...
- ServiceFramework作为Java Web框架都有哪些不错的设计
前言 最近需要开发一个纯API的项目,mlsql-cluster,从无到有,到最后完整的proxy功能开发完毕,只花了四个小时不到,自己不尽小感叹了一把 ServiceFramework的高效. 关于 ...
- TestOps - 最健壮性的测试角色
一十一 发表于 2018-03-02 09:10:08 TestOps 最具影响力的测试运维一体化综合平台. DevOps实现了从代码到服务的快速落地,而TestOps集成了DevOps效率,更是 ...
- vue路由传参的三种基本方式
现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据. 父组件中: <li v-for="article in articles" @click= ...
- XML语言学习随笔
XML和HTML都是W3C的定制的标准,XML的诞生本身是为了替代不成熟的HTML,但是因为现实的环境,XML替代HTML并未成功.之后W3C为了代码严谨性的决心,又发布了升级版的标记语言XHTML, ...
- TFS 安装遇到的问题
居然是是微信桌面客户端占用了8080端口,也是醉了... 1 VS链接 源码管理器 发现提示 Http 404, 发现原来是自己吧tfs 给删除了 2 重新安装tfs,过程中提示 8080 端口被占用 ...
- Flume Source 实例
Flume Source 实例 Avro Source 监听avro端口,接收外部avro客户端数据流.跟前面的agent的Avro Sink可以组成多层拓扑结构. 1 2 3 4 5 6 7 8 9 ...
- 【redis专题(5)】命令语法介绍之sets
标签(空格分隔): Redis 关于 redis的无序集合有三个特点: 无序性, 确定性(描述准确) , 唯一性: 有点类似于数据容器: 增 SADD key member1 [member2] 作用 ...
- [20171214]hashcat破解oracle口令.txt
[20171214]hashcat破解oracle口令.txt hashcat is the world's fastest and most advanced password recovery u ...