题目
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\lt N\le300)$ 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(\le100)$ 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,喜欢一个人D,A害羞不好意思直接和D说话,A会去找A的同性好朋友B,然后B去找B的好朋友C,但是前提是C是D的同性好朋友。也就是A和B是同性朋友,C和D也是同性朋友,只要B和C也是朋友,就满足条件。但要排除 A和D直接是朋友的情况。
思路
- 关系的输入不能用int,因为题目说用
'-'
号表示女生,而并不是负数表示女生,也就是说-0000,0000是不能用int
判断性别的,所以必须用字符串输入; - 后期优化意识到,这道题并不需要记录性别,题目思路首先是找到A的朋友,这些朋友中,女生是不参与这个过程的,所以在输入的时候将女生过滤掉,我们用字符读取,所以当字符长度相同时就是同性;
- 时间上的优化,以空间为代价换取时间,定义一个二维数组记录A、B是否是朋友,用
vector
记录A的所有同性朋友;
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int transf(string str){
int res = 0;
int i = 0;
if(str[0] == '-')
i++;
for(; i<str.length(); i++){
res = res*10 + str[i]-'0';
}
return res;
}
int abs(int a){
if(a >= 0)
return a;
return a*-1;
}
bool cmp(pair<int,int> a, pair<int,int> b){
return a.first != b.first ? a.first < b.first:a.second < b.second;
}
int mp[10000][10000];
vector<vector<int> > p(10000);
int main(){
int n, m, k;
scanf("%d%d", &n, &m);
string s1, s2;
for(int i=0; i<m; i++){
int a, b;
cin>>s1>>s2;
a = transf(s1), b = transf(s2);
if(s1.length() == s2.length())
p[a].push_back(b), p[b].push_back(a);
mp[a][b] = mp[b][a] = 1;
}
scanf("%d", &k);
vector<pair<int,int> > ans;
while(k--){
int a, b;
scanf("%d%d", &a, &b);
a = abs(a), b = abs(b);
for(int i=0; i<p[a].size(); i++){
for(int j=0; j<p[b].size(); j++){
if(p[a][i] == b || p[b][j] == a)
continue;
if(mp[p[a][i]][p[b][j]] == 1){
ans.push_back(pair<int,int>(p[a][i], p[b][j]));
}
}
}
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", ans.size());
for(int i=0; i<ans.size(); i++){
printf("%04d %04d\n", ans[i].first, ans[i].second);
}
ans.clear();
}
return 0;
}