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 ... 
随机推荐
- Swagger2限定接口范围
			前面在使用Swagger2时遇到的坑中简单介绍了Swagger的使用. 不过默认情况下,Swagger2会把项目中的所有接口都展示在列表里,特别是你用了Springboot/SpringCloud之后 ... 
- STOMP
			STOMP: 说明:STOMP is a simple text-orientated messaging protocol. 面向文本消息协议 spring 之stomp https://www.c ... 
- JavaScript基础要点
			一.值和类型及运算 JavaScript中的六种基本值类型 数字(number).字符串(string).布尔值(boolean).对象(object).函数(function).未定义类型(unde ... 
- 从gitHub上拉取并运行项目
			今天我们来试一下如何从gitHub上拉取一个项目并且运行起来,话不多说,我们直接开搞可好 1.首先我们先获取到项目地址(此处我以自己的项目地址作为示例) 我们选择红圈处的clone or downlo ... 
- 【代码笔记】Web-HTML-简介
			一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ... 
- 数据库开源框架GreenDao的使用解析
			数据库开源框架GreenDao的使用解析 1,GreenDao概述 1),greenDao是一个当下十分火热的数据库开源框架,或者说是一个帮助Android开发者将数据存到SQLite中的一个开源项目 ... 
- 《ASP.NET MVC企业实战》(三)MVC开发前奏
			 在上一篇“<ASP.NET MVC企业级实战>(二)MVC开发前奏”中跟随作者大概了解了一些C#3.0和3.5中的新特性.本篇继续以这样的方式来学习C#中的一些特性. 一.C#3. ... 
- Quill编辑器IOS下无法获取焦点的解决方法
			造成Quill-Editor无法获取焦点的大部分原因是Css的问题,罪魁祸首: *{ -webkit-user-select:none; } ios下直接造成无法获取焦点. 解决方法,覆盖以上css设 ... 
- The packaging and installation process of Android programs
			D:\android\adt-bundle-windows-x86-20131019\sdk\platform-tools工具的路径. 安卓工程经过eclipse编译然后通过aapt工具打包生成一个. ... 
- JS笔记(三):数组、函数、类
			(一) 数组 //创建数组 var the_array = [1,2,3,4,'5'] console.log(the_array[0]) //读取索引为0的数据 the_array[5] = '赋值 ... 
