PAT 甲级 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 -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
题目分析见 A1139
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int N,M;
const int gap=10000;
struct fri{
vector<int> friends[2]; //分别时男同学和女同学
};
fri v[20005];
vector<pair<int,int> > ans;
int toInt(char s[])
{
if(s[0]=='-')
return gap-atoi(s);
else
return atoi(s);
}
void toFind(char s1[],char s2[])
{
int a=toInt(s1);
int b=toInt(s2);
int sexA=0,sexB=0;
if(s1[0]=='-')
sexA=1;
if(s2[0]=='-')
sexB=1;
int i,j,k,t;
for(i=0;i<v[a].friends[sexA].size();i++) //枚举A的同性朋友
{
int friA=v[a].friends[sexA][i]; //A的朋友
if(friA==b) //直接就是b
continue;
for(j=0;j<v[b].friends[sexB].size();j++)
{
int friB=v[b].friends[sexB][j]; //B的朋友
if(friB==a) //直接就是a
continue;
//下面判断friA和friB是否是朋友关系
for(k=0;k<v[friA].friends[sexB].size();k++)
{
t=v[friA].friends[sexB][k];
if(t==friB)
ans.push_back(pair<int,int>(friA,friB));
}
}
}
}
void toChange()
{
for(int i=0;i<ans.size();i++)
{
if(ans[i].first>=10000)
ans[i].first-=10000;
if(ans[i].second>=10000)
ans[i].second-=10000;
}
}
int main()
{
scanf("%d%d",&N,&M);
char s1[8],s2[8];
for(int i=0;i<M;i++)
{
scanf("%s%s",s1,s2);
int a=toInt(s1);
int b=toInt(s2);
if(a>=10000)
v[b].friends[1].push_back(a);
else
v[b].friends[0].push_back(a);
if(b>=10000)
v[a].friends[1].push_back(b);
else
v[a].friends[0].push_back(b);
}
int query;
cin>>query;
while(query--)
{
ans.clear();
scanf("%s%s",s1,s2);
toFind(s1,s2);
toChange();
sort(ans.begin(),ans.end());
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++)
printf("%04d %04d\n",ans[i].first,ans[i].second);
}
return 0;
}
标签:1139,PAT,int,2002,2003,Contact,first,friends,2001 来源: https://blog.csdn.net/qq_41173604/article/details/100535690