1139. First Contact (30)
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 -2003Sample Output:
4 1002 2004 1003 2002 1003 2003 1004 2002 4 2001 1002 2001 1003 2002 1003 2002 1004 0 1 2003 2001 0
虽然没有全部刷完,但我把能写的都写了,祝我2018.3.18PAT好运吧~
---------------------------------我是好运分割线-------------------------------------------------------------------------------
-------------------------------哇咔咔,2018.3.18这次考了94~----------------------------------
这题考试的时候看了下,就不会做。。。
仔细看的话 没什么技术含量,本质是邻接的问题。就是处理下标麻烦了点
能看出 0000 和 -0000不同(测试点2)的都是dalao!!!!! 我是佩服那些考试的满分玩家的
#include<stdio.h>
#include<string>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int person;
map<string,int>mp1;//id 映射 下标
map<int,string>mp2;//下标 映射 id
vector<int>friends[310];
int adj[310][310];
struct node{
int c1,c2;
};
int getindex(string c){
if(mp1.find(c)==mp1.end()){
mp1[c]=person++;
return person-1;
}
else{
return mp1[c];
}
}
int samegender(string c1,string c2){
if(c1.size()==c2.size())return 1;
return 0;
}
bool cmp(node a,node b){
if((a.c1)!=(b.c1))return (a.c1)<(b.c1);
return (a.c2)<(b.c2);
}
int main(){
int n,m,i,index1,index2,k,j;
string c1,c2;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++){
cin>>c1>>c2;//字符串输入 测试点好像很可能有 0000 -0000
index1=getindex(c1);//转化对应下标
index2=getindex(c2);
friends[index1].push_back(index2);//邻接表
friends[index2].push_back(index1);
adj[index1][index2]=adj[index2][index1]=1;//邻接矩阵
mp2[index1]=c1;//下标映射 id
mp2[index2]=c2;
}
scanf("%d",&k);
while(k--){
vector<int>a,b;//a存储 能联系到第一个人 并且与第一个人性别相同
vector<node>ans;//b存储 能联系到第二个人 并且与第二个人性别相同
cin>>c1>>c2;
if(mp1.find(c1)==mp1.end()||mp1.find(c2)==mp1.end()){
printf("%d\n",0);continue;//不在输入数据中
}
index1=mp1[c1];
index2=mp1[c2];
if(samegender(c1,c2)==0){//异性
for(i=0;i<friends[index1].size();i++){
if(samegender(c1,mp2[friends[index1][i]])){
a.push_back(friends[index1][i]);
}
}
for(i=0;i<friends[index2].size();i++){
if(samegender(c2,mp2[friends[index2][i]])){
b.push_back(friends[index2][i]);
}
}
}
else{//同性 ,注意 如果A->B,则A不能直接把B加入 ,需要通过其他人
for(i=0;i<friends[index1].size();i++){
if(samegender(c1,mp2[friends[index1][i]])){
if(friends[index1][i]!=index2)
a.push_back(friends[index1][i]);//则A不能直接把B加入
}
}
for(i=0;i<friends[index2].size();i++){
if(samegender(c2,mp2[friends[index2][i]])){
if(friends[index2][i]!=index1)
b.push_back(friends[index2][i]);//则B不能直接把A加入
}
}
}
for(i=0;i<a.size();i++){
for(j=0;j<b.size();j++){
if(adj[a[i]][b[j]]){//A的朋友与B的朋友邻接
node tmp={abs(stoi(mp2[a[i]])),abs(stoi(mp2[b[j]]))};//..
ans.push_back(tmp);//保存答案
}
}
}
sort(ans.begin(),ans.end(),cmp);
printf("%d\n",ans.size());
for(i=0;i<ans.size();i++){
printf("%04d %04d\n",ans[i].c1,ans[i].c2);
}
}
}